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).

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

I really need help with this python question. You can't use break, str.endswith, list.index, keywords like: await, as, assert, class, except, lambda, and built in functions like: any, all, breakpoint, callable.

 

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:

  1. Empty
  2. 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

5

1

2

4

7

3

 

Coding Standards

  1. Constants above your function definitions, outside of the "if __name__ == '__main__':" block.  
    1. A magic value is a string which is outside of a print or input statement, but is used to check a variable, so for instance:
      1. print(first_creature_name, 'has died in the fight. ') does not involve magic values.
      2. However, if my_string == 'EXIT':  exit is a magic value since it's being used to compare against variables within your code, so it should be:
        EXIT_STRING = 'EXIT'

...
if my_string == EXIT_STRING:

  1. A number is a magic value when it is not 0, 1, and if it is not 2 being used to test parity (even/odd).
  2. A number is magic if it is a position in an array, like my_array[23], where we know that at the 23rd position, there is some special data.  Instead it should be
    USERNAME_INDEX = 23

my_array[USERNAME_INDEX]

  1. Constants in mathematical formulas can either be made into official constants or kept in a formula.  
  1. Previously checked coding standards involving:
    1. snake_case_variable_names
    2. CAPITAL_SNAKE_CASE_CONSTANT_NAMES
    3. Use of whitespace (2 before and after a function, 1 for readability.)
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Linked List Representation
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education