During week 4, I learnt more about recursive functions. Specifically, I learnt about nested list and how to solve for the depth of a nested list. We started by defining the nesting-depth of L as 1 plus the maximum nesting depth of L's elements if L is a list, otherwise 0. Then I learnt that the code we write is almost exactly the same as the definition, that is, return (1 + max([nested_depth(x) for x in L] + [0]) if isinstance(L, list) else 0). we add [0] into L because we need to treat the depth as 0 in case we go through an empty list.
At first I was a little confused about what's going on because I don't quite understand why and how we can put a function header in its body to execute the code. However, by discussing with the person next to me during class and also by finishing up Assignment 1, I walked myself through both the definition and application of recursive functions. What's more, figuring out how to complement the codes in the Tour.py file in Assignment 1 gives me a more thorough understanding of recursive functions.
What I found most interesting about week 4's lectures was the turtle functions and how amazingly the "turtle" object can draw those pictures. As a very classic recursive object, turtle also refer to itself in the function body. the picture it drew impressed me a lot. The basic rule of how the turtle object works is like the growth of tree twigs, which is a lot like the picture below:
Recursive functions are amazing and hopefully I'll learn more about them in the next few weeks.
SEE YOU NEXT WEEK! :)

没有评论:
发表评论