Monday, 2 March 2015

Week 7 Recursion

  In these weeks we go through the topics about recursion and how to use recursion in programming. Recursion is a method which will call itself when I operating it. I thought it likes while loop because both of them keep looping until the target is met. Overall, recursion is a useful method to evaluate the nested lists and nested abstract object, which means it will contain a same datatype value in its self. My first image about recursion is that this topic must be very hard to understand, because unlike other methods, recursion is very abstract, it is hard to image that how recursion is working? My recursion is working well or not? After the lectures, I sort out the steps to write a recursion code.
  First of all, I have to assume the parameter did not contain a data with a same datatype inside it such as a normal list but not a nested list. My code will go through all normal elements inside the list then return a simple result such as a number. After that, I assume that if the parameter is a nested list, I will apply this function again on every elements in the nested list. If the element is a normal, it will return a simple value. If the elements is a list or nested list, the function will apply on them again until there are no more data need to be evaluated. After all, I can evaluate all data in the nested list and return the results that I want.
  Although I have method it write a recursion code, it may not make the coding easy. In lab5 we use tree-related code to do the recursion. One assignment in the lab is challenging:
 def list_if(t, p):
    ''' (Tree, function) -> list
    Return a list of values in Tree t that satisfy p(value)
    >>> def p(v): return v > 4
    >>> t = descendents_from_list(Tree(0), [1, 2, 3, 4, 5, 6, 7, 8], 3)
    >>> L = list_if(t, p)
    >>> L.sort()
    >>> L
    [5, 6, 7, 8]
    >>> def p(v): return v % 2 == 0
    >>> L = list_if(t, p)
    >>> L.sort()
    >>> L
    [0, 2, 4, 6, 8]
'''
The target is compare each number in the tree, return a list of number which is larger than p. At first, I created an empty list named new, and if the tree does not have a children and the data is larger than p, the data will be appended:
    new = []
    if len(t.children) == 0 and p(t.value):
        new.append(t.value)
After that, I start to consider the situation which is that a tree with branches of children. First I have to evaluate its own value, than I call this function itself on the children tree:
else:
        if p(t.value):
            new.append(t.value)
        new.extend(gather_lists([list_if(n, p) for n in t.children]))
    return new
The tree is different from the nested list, I have to evaluate its own value otherwise I will not get right results.

   In this week I started to write the assignment 2. When I considering the recursion code in the minimax strategy I felt it is very hard. So I visit some slogs of my classmates to get some advice. In Zhou's blog(http://csc148wendezhou.blogspot.ca/), he suggests that consider more about the BTNode and its recursion method will be helpful to complete assignment. I am not sure that his method is useful. But it still helps me a lot because it notices me that I can create a new tree-related class to store all moves, and the moves after previous move in my tippy game.