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 How Deep is the List? Write a recursive function: def howdeepliststruct: 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 since it's a single list. The depth of is since there are lists within lists, but both of those lists are at the same depth. The depth of is because the first sublist is depth then then again, and then Therefore The depth of any list is considered to be the max of the depths of the sublists 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 andor 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 maxdepth. It won't always produce something with that depth, but it will produce something with depth maxdepth. 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 makeliststructuremaxdepth, p: if maxdepth and random.random p: newlist for i in range: sublist makeliststructuremaxdepth p if sublist is not None: newlist.appendsublist return newlist return None if namemain: printhowdeep printhowdeep printhowdeep printhowdeep printhowdeep printhowdeep Sample Output linux python howdeep.py
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
