Question: Challenge problem: Deep Cleaning with Recursion! This problem demonstrates something that truly requires recursion! The removeAll function above does this: def removeAll ( e ,

Challenge problem: Deep Cleaning with Recursion! This problem demonstrates something that truly requires recursion! The removeAll function above does this:
def removeAll(e, L):
'''takes in an element e and a list L. Then, removeAll should return another list that is identical to L except that all elements identical to e have been removed'''
if len(L)==0:
return []
if L[0]== e:
return removeAll(e, L[1:])
else:
return [L[0]]+ removeAll(e, L[1:])
>>> removeAll(42,[42,67,42,[42,42,43],47])
[67,[42,42,43],47]
In other words, in this example, it removes all of the 42's from the list, but it does not remove 42's that are embedded in lists within that list. Your job here is to write a function called deepRemoveAll that scours its input and removes not only the desired item, but also removes that item deep inside other lists. Here are a few examples:
>>> deepRemoveAll(42,[42,67,42,[41,42,43],47])
[67,[41,43],47]
>>> deepRemoveAll(47,[42,47,[1,2,[47,48,49],50,47,51],52])
[42,[1,2,[48,49],50,51],52]

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!