2014年3月1日星期六

Week #7 --- Recursion

In the last few weeks, we learn about the recursion deeply from lectures, reading materials, labs and assignment 1. Base on my comprehension, recursion means we call the function inside itself for solving the subproblem easily. The recursion makes the function always run and stop until finding the result of subproblem.

When I knew recursion in the lecture first time, I had no ideas for this term, because it seemed very abstract and hard to understand. After reading the material from the course website, though I knew what is the recursion, I still did not know how to apply recursion in python so that I felt difficult to finish the lab exercise in the lab. Talking about the lab, I was really appreciated to my partner since he helped me lot. He told me a method for understanding recursion easily. When I get a question, I will seperate the question into two parts. The first part is basecase which means the normal case. The second part is recusion case. I want to use an example to explain what is basecase and recursion case.

For example(This is an example from the week 5's lecture):
def rec_max(L: list) -> float:
    """Return the max number in possibly nested list of numbers.

    >>> rec_max([17, 21 0])
    21
    >>> rec_max([17, [21, 24], 0])
    24
    >>> rec_max([17, [21, 24], [18, 37, 16], 0])
    37
    """

    return max([rec_max(x) if isinstance(x, list) else x for x in L])

In this case, the basecase is when L is a normal case and L do not contain any nested list. The rec_max function will only run once time. Therefore "else x" is basecase. When L contains some nested list, we need to run rec_max function in the nested list . This case is recursion case. Therefore, we write the recursion case "rec_max(x) if isinstance (x, list)". When x is nested list, we need to call the rec_max function again. This is what recursion means. However, "for x in L" is the condition of the function which can make the function stop when every element in L executed. This method is my partner teaching me which is a good method for me to solve the recursion problem. 

In the assignment 1, we did a good application of recursion. We use recursion to finish the 4-stools hanoi tower game. This is the first assignment of CSC148. I think the most difficult part is part 5 --- calculate the minimum moves of 4-stools cheeses. This is a kind of recursion problem. I spent almost two days finishing part 5 of this assignment with my partners. We discussed together, search some reading source from internet. We seperated the problem into two small parts. First, we though about the minimum of cheeses move in 4-stools. Then, we write the code for the number of moves. Finally, we combined two small parts in the main code. During the process, we got a lot of error. We modified the code continuously until we pass the testcase. When we finished the recursion of part 5, we were screaming at that moment. We were excited we can overcome the challenge. After I finished the assignment 1, my biggest experience is that the most difficult part is recursion and we achieve a good application of recursion this time. 

Although we practiced a lot about recursion in the lab, assignment 1 and term test 1. I think we still need to practice more different recursion problem for comprehencing recursion more deeply in the further study. 

没有评论:

发表评论