This is my iterative solution:
def exists(key, arg):
if not arg:
return False
else:
for element in arg:
if isinstance(element,list):
for i in element:
if i==key:
return True
elif element==key:
return True
return False
print(exists("f", ["a", ["h", "e", "j"], ["t", "e", "s", "c", "o"]]))
However, my L.A. wants a double recursive function to solve this.
my attempt:
def exists(key, arg):
if not arg: //base case
return False
elif arg[0]==key: //if we find the key from the first trial
return True
else:
return (exists(arg[0:],key))
This doesn't work; it shouldn't, because there is no stop. Also, it does not account for lists of lists; I don't know how to do that.
Any answer, comment, etc. is appreciated
arg[0:] == arg. Your recursive call needs to be onarg[1:]