Question: Recursive list nesting check Lists can hold integers, strings, and even other lists, for example [ [test], [ ], [1, [me] ] ) is a
Recursive list nesting check
Lists can hold integers, strings, and even other lists, for example [ ["test"], [ ], [1, ["me"] ] ) is a list with three elements: a list ("test"], an empty list [ ], and a list [1, ["me"] ]. When objects like lists or dictionaries contain objects of the same type as themselves, we say the inner objects are nested in the outer objects. For an object in a nested list, we will call the number of enclosing outer lists the depth of that object. For example, in the list [ ["test"], [ ], [1, ["me"] ) ), the list ["test"] is at a depth of 1, the string "test" is at a depth of 2 and the string "me" is at a depth of 3. Write a recursive function to calculate the maximum depth of any object in a given list. Name the function maximum_nesting_depth(x), where x is a list, and return an integer representing the maximum depth of any object in X You can assume the test cases will not be the empty list Examples maximum_nesting_depth ( ["test"], [], [1, ["me"] } ]] = 3 maximum_nesting_depth(["test"]) = 1 maximum_nesting_depth([ 1, ["me"] ] = 2 Note that empty lists will not contribute to the nesting depth; we are computing the maximum nesting depth of any object maximum_nesting_depth([ "test", [ 1 ] is 1 because "test" is at depth 1 maximum_nesting_depth([ "test", [[ ]] ] is 2 because[ ] is at depth 2 and empty Hint 1: You can use the following recursive definition: Base Case: If x is a list containing no lists, the maximum nesting depth is 1. Recursive Case: If x is a list containing a list with maximum nesting depth of n, then x has a maximum nesting depth of at least n+1. Hint 2 For an objecte the expression isinstance(e, list) will be True if and only ife is a list. 295818.710262. LAB ACTIVITY 10.10.1: LAB: Max Depth of Nested Lists 2/3 main.py Load default template 1 # TO DO: write maximum_nesting_depth function 2 def maximum_nesting_depth(x): 3 #Base case 4 5 #Recursive step 6 7 return 9 if le 11 name "main_": x - int (input()) #print(axiunun_nesting_depth(x)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
