Given a nested list L (such that each element of L is either an integer, or a list, which may itself contain integers, or lists, which may in turn.... etc) return True i s is in L.
search([1, [2, 3], 4, [5, [6 , [], [8, 9]], 10]], 8)
should return True.
Here is what I have so far:
def search (L,s):
"""(list, anytype) _> Bool
Returns true iff s is present in L
"""
if L:
if L[0] == s:
return True
else:
return search (L[1:], s)
else:
return False
This current code works for a list if it isn't nested, or if it is nested like so (the nested element is the last element):
[1, 2, 3, [4, 5]]
But not for the following:
[1, 2, [3, 4], 5]
How can I change my code so that it works for a nested list? with the nested element being anywhere in the list not strictly the last element?
Appreciate any help!
EDIT: Sorry, forgot to specify that it needs to be recursive.