Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Exercise 6.3 (nested.py) A nested list is a list that contains one or more lists as elements. For example, the list [1,2,3, [50,60,70], CC8888],999],10] contains

image text in transcribed
Exercise 6.3 (nested.py) A nested list is a list that contains one or more lists as elements. For example, the list [1,2,3, [50,60,70], CC8888],999],10] contains 6 elements. The first three elements are integers. . The fourth element is a list: [50,60,70] The fifth element is also a list. This list has two elements: another list (L (8888],999]) and 10. The following functions should have recursive definitions. 1. Write a function named element_of that returns True if the first argument is within any of the sub-lists of the nested list and False otherwise. 2. Write a function named filter_by_depth that takes two arguments: an integer repre- senting depth and a nested list. It should remove all sub-lists that are more than depth deep 2 nested. py ? $. python3 4 >>> import nested s>nested.element of (5, [1,2,3,4,5,6,7]) True 7>>>nested.element of (7, [1,2, [3,4, [5,6]1, 07]]) s True 9 >nested.element of (77, [1,2, [3,4, [5,611, [7]1) o False >>> nested.filter_by_depth (0, [1,2,3]) 12 3 >>>nested.filter_by_depth(1, [1,2,3]) s >nested.filter_by_depth (5, [1,2,3]) 16 [1,2,3] 17 >>> nested.filter_by_depth (2, [1,2, [3,4, [5,611,0711) s [1,2, [3,41, 0711 Hint: use the type function to check if an element is a list

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

Students also viewed these Databases questions