I think I misunderstand the topic of the second week so I changed it into recursion. The original one is about the process of how we did a1 but I'm really sorry that I didn't see the topic clear.
Recursion:
I found it really helpful to think from the really basic examples. I'll use the function nested_depth(L) as a example. The basic example is a non-list object and the function should return 0. Then the second basic example is a non-nested list. The function should return 1. And then the more complecated example, nested-depth one list. We should recurse function nested_depth on the inner- list and add that number to the outer-list and got 2.
Examples that provided on the slide:
>>> nested_depth(3)
0
>>> nested_depth([1, 2, 3])
1
>>> nested_depth([1, [2, 3], 4])
2
Since there might be one or more inner-lists and their nested-depth might be different, the method max should be used.
So we got:
def nested_depth(L):
"""
Return nested depth of list L.
"""
return (1 + max([nested_depth(x) for x in L] + [0])
if isinstance(L, list) else 0)
So what really important is the examination of the basic cases and some of the special cases. And as moving on to the more completecated cases, it would be clearer that how and where to use recursion.