Kodewick
Start learning →
Basic←→0 / 80

Family generations

#066BasicRecursionDicts5 min

In a family tree each person is a dict: name is their name and kids is a list of their children (dicts just like this). Write generations(person): how many generations from this person down. Someone with no children is 1 generation.

Examples
generations({"name": "Gran", "kids": [{"name": "Mum", "kids": [{"name": "Lu", "kids": []}]}, {"name": "Tom", "kids": []}]})→3
generations({"name": "Pip", "kids": []})→1
main.py
def generations(person):
    deepest = 0
    for kid in person["kids"]:
        # your code here: ask how many generations this kid has, and keep the biggest
        pass
    return 1 + deepest
Ctrl + Enter runs too
Result

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