Kodewick
Start learning →
Mini challenge←→0 / 80

Where are the keys?

#068Mini challengeRecursionLists7 min

A bag holds things (strings) and smaller bags (lists), and bags can hold bags. Write find_keys(bag): how deep are the "keys"? Right in this bag is level 1, inside a bag in it is level 2, and so on. If they aren't there, return None. There's only one set of keys.

Examples
find_keys(["wallet", ["phone", "keys"]])→2
find_keys(["keys", "gum"])→1
main.py
def find_keys(bag):
    if "keys" in bag:
        return 1
    for thing in bag:
        if isinstance(thing, list):
            # your code here: look in this smaller bag; if found, add 1 to the level and return it
            pass
    return None
Ctrl + Enter runs too
Result

Run tries the examples. When they look right, Submit.