This week, we delved deeper into recursion. We looked at it mostly in terms of lists, creating a recursive function "nested_depth" that calculated the nested depth of a list. For example, the nested depth of [1] is 1, and the nested depth of [1, [2, [3, 4], 5], 6] is 3. The return statement for this example is as follows:
return (1 + max([nested_depth(x) for x in L] + [0]) if isinstance(L, list) else 0)
This, at first, was a little tough to comprehend. It definitely took a bit of mental wrestling with the fact that a function could make a call on itself before it was even fully defined. However, I think I made a bit of a personal breakthrough this week. I did some extra research on the internet and found that the more I looked at different examples of recursive functions the more sense the concept made to me.
So, this was a good week in CSC148. I feel comfortable with the material so far. Eager to learn more.
No comments:
Post a Comment