2016年3月3日星期四

Week 7


SLOG Week 7

In last week’s lecture, I learned about recursion, which I think is a very interesting and useful topic. In the class, the professor gave us several practice of recursion. I think it is hard. It is hard to figure out the base case and hard to follow the recursive steps. I find writing down each step of recursion is a good way to understand a recursive function.

Also the recursive examples on the website helped me to understand the recursive concept a lot. I wrote down every step for each example, such as nested_counts and nested_contains. Sometimes I did not write the correct step, than the result was incorrect. I  think I am improving after doing the exercise.

To write the recursion code, the most important thing is writing a good base case. The base case is what the recursive steps calling at each time of recursion. And the code always follows this format:


             def f(obj):
                 if isinstance(obj, int):
          ...
    else:
         for lst_i in obj:
            ... f(lst_i) ...


The base case should appeared in the if part, and the else part is the recursive calls. If the recursive function is not correct, then the base case and recursive calls might be incorrect. I think I am going to use recursion many times in assignment 2.