Just like the turtle we learnt about last week during lecture, the very representative object called Tree during week 6's lecture inspired me a lot. I discovered that the intellectual merit of recursive functions is that it allows us to write the code in a optimized and efficient way when encountering difficult situations.
What's more, the most fascinating thing about recursive functions is tracing through them. For instance, by learning about the three traversals of a Tree object- preorder, inorder, and postorder, I saw that tracing through recursive functions can be sort of tedious, however, eventually I can reach the base case and solve for the correct answer.
Similarly, the lectures on LinkedList object during week 7 are also very informative. Conceptually, there's a sequence of nodes within a linked list, each with a head and a reference to the rest, which is also a linked list. As shown below, a linked list is basically a nested list with references.
When initializing the object, we used the default value None for both the head and rest. And surprisingly, prepending a new head to a linked list is not that simple as I thought, there're three procedures:
1. Start the rest of the list with the current attributes by shallow copying them
2. Change the current head to the one passed in
3. Change the current rest to the copy.
Writing the __contains__ builtin function also helps me understand the structure of a LinkedList object.

Tracing through a recursion is not really a good idea...
回复删除