Question: how should i go about answering this problem? Problem 5 - How Deep is the List? Write a recursive function: def how _ deep (

how should i go about answering this problem? Problem 5- How Deep is the List? Write a recursive function: def how_deep(list_struct): You will be passed a list. However this is not a generic list, it will conform to these specific rules. A list will either be: Empty A list of lists. This means for example that a list can either be: [], or [[],[],[]], or [[[],[],[],[[[]]]],[]] Our goal is to calculate the depth of these lists. It must be done recursively. The depth of [] is 1 since it's a single list. The depth of [[],[]] is 2 since there are lists within lists, but both of those lists are at the same depth. The depth of [[[]],[],[[]],[[[]]]] is 4 because the first sublist is depth 2, then 1, then 2 again, and then 3. Therefore 1+3=4. The depth of any list is considered to be the max of the depths of the sublists +1.(This is the definition you should use for the purposes of the recursion). Hint: You'll need a for loop in your recursive case. For this problem, you may add helper functions if you want to, and/or add ONLY DEFAULT parameters to the recursive function (again, if you want). They must work with the given driver code exactly. Test Driver and Starter Code Here I've included a little function which produces samples to test out with the max_depth. It won't always produce something with that depth, but it will produce something with depth <= max_depth. Feel free to use this code to make new test cases and to test them out on your function. The test driver code is: import random def make_list_structure(max_depth, p=.8): if max_depth and random.random()< p: new_list =[] for i in range(5): sub_list = make_list_structure(max_depth -1, p *.9) if sub_list is not None: new_list.append(sub_list) return new_list return None if __name__=='__main__': print(how_deep([[[],[],[],[[[]]]],[]])) print(how_deep([])) print(how_deep([[],[]])) print(how_deep([[[]],[],[[]],[[[]]]])) print(how_deep([[[[],[[]],[[[]]],[[[[]]]]]]])) print(how_deep([[[],[]],[],[[],[]]])) Sample Output linux5[109]% python3 how_deep.py 512473

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!