Part of an algorithm course I am taking is to use look through a dictionary of keys and values and continue to look for matching keys & values. For example if there is a matching key, use the value to look up the next key and continue down the rabbit hole. If a match if found more than once to prevent an infinite loop return False else it should return the last key found.
- Look up a key called "bat", and find a value "pig" then continue
- Look up a key called "pig", and find "cat" then continue
- Look up a key called "cat", and find "dog" then continue
- Look up a key called "dog", and find "ant" then continue
- Look up a key called "ant", and find no associated value, and so it would return "ant"
This is the data:
d = {'bat': 'pig', 'pig': 'cat', 'cat': 'dog', 'dog': 'ant', 'cow': 'bee', 'bee': 'elk', 'elk': 'fly', 'ewe': 'cod', 'cod': 'hen', 'hog': 'fox', 'fox': 'jay', 'jay': 'doe', 'rat': 'ram', 'ram': 'rat'}
This is my function below where I am trying learn recursion...and understand why my function doesn't return False
when a key saved to MEMORY
has a value greater than 2.
def rabbit_hole(dictionry,key,MEMORY={}):
#print(MEMORY)
if key in MEMORY:
MEMORY[key] += 1
else:
MEMORY[key] = 1
for val in MEMORY.values():
#print(val)
if val == 2:
return False # <--- why not working?
for k,v in dictionry.items():
if k == key:
# recursive call
rabbit_hole(dictionry,v,MEMORY)
#print("return last added dict key")
return list(MEMORY)[-1]
Any tips appreciated not a lot of wisdom here...if I run:
print(rabbit_hole(d, "bat"))
print(rabbit_hole(d, "ewe"))
print(rabbit_hole(d, "jay"))
print(rabbit_hole(d, "yak"))
print(rabbit_hole(d, "rat"))
The code above returns:
ant
hen
doe
yak
ram
But its supposed to return a False
on rat
like this:
ant
hen
doe
yak
False