Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In python, please. below is the list_node.py: below is the test case and result: 5 array_to_list recursive (data) Include this function in the file linked_list_recursion_part_2.py

In python, please.image text in transcribed

below is the list_node.py:

image text in transcribedbelow is the test case and result:

image text in transcribedimage text in transcribed

5 array_to_list recursive (data) Include this function in the file linked_list_recursion_part_2.py . Repeat the problem from the previous project - but this time, you must do it recursively. Remember to handle the corner cases! This function must be recursive, but is not required to obey the Annoying Requirements. 5.1 Hints If you ever find yourself thinking, How do I put a node on the tail of the list?" you're probably going about this backwards. To recursively build a list, we will usually (if possible) build a smaller list, and then add one more element at the head. How would you recurse into the array, in order for that to be possible? list_node.py Contains a simple ListNode class, which simply has 'val' and 'next' fields. II II II class ListNode: Models a single node in a singly-linked list. than the constructor. Has no methods, other IIIIII def II II 11 init__(self, val): Constructs the object; caller must pass a value, which will be stored in the 'val' field. self.val = val self.next = None def str__(self): vals = [] objs = set() curr = self while curr is not None: curr_str = str(curr.val) if curr in objs: vals.append("{} -> ... (to infinity and beyond)".format(curr_str break else: vals.append(curr_str) objs.add(curr) curr = curr.next return " -> ".join(vals) import list node inport linked_list_recursion_part2 FFFFF#### INPUT AND EXPECTED OUTPUT yyyyypp # INPUT: in data = [-1, "asdf","fon","bar",2,1234 ] ### TEST CODE yyypp der rain{1} print("Testing array_to_list_recursivel)...") print() print(t"Input data: (in_data") aut_list = linked_list_recursian_part2.array_to_list_recursiva in data) print("Returned list: uut list)"} print ) print("Checking to make sure that all of the nodes in the output list are') print at the proper type - that is, listNode.) print {1 # chack that the returned value is a list curr - out_list while curr is not None: if not isinstance(curr, list_node. ListNode): print("ERROR: One ar mare nodes is not of the ListNade class. NOTE: This can happen if you copy the ListNode class into your own file, instead of using the version inside list_node.py") break curr = Curr.next print("Output list node check complete.") print() print("TESTCASE COMPLETED) if __naimg. main( = "__main__": Testing array_to_list_recursive()... Input data: [-1, asdf', 'foo', 'bar', 0, 1234] ] Returned list: -1 -> asdf -> foo -> bar -> O -> 1234 Checking to make sure that all of the nodes in the output list are of the proper type that is, ListNode. 4 Output list node check complete. TESTCASE COMPLETED

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Management Systems Designing And Building Business Applications

Authors: Gerald V. Post

1st Edition

0072898933, 978-0072898934

More Books

Students also viewed these Databases questions

Question

Are the margins ample? (293)

Answered: 1 week ago