Saturday, 7 March 2015

Week 7: Recursion

I thought we do not need to write the slog in reading week, sorry about the late!

We have learned the class Tree and using recursive function to solve the lab exercise. To be honest, I feel like the first four functions: list_all, list_leaves, list_interior, list_if are not very difficult.  I used similar way to write list_all, list_leaves, list_interior. I constructed a list firstly, then use if and else statements to discuss the simplest situation and complex situation as showed below:  

def list_all(t):
    ''' (Tree) -> list
 
    Return a list of values in t.
 
    >>> t = Tree(0)
    >>> list_all(t)
    [0]
    >>> t = descendents_from_list(Tree(0), [1, 2, 3, 4, 5, 6, 7, 8], 3)
    >>> L = list_all(t)
    >>> L.sort()
    >>> L
    [0, 1, 2, 3, 4, 5, 6, 7, 8]
    '''
    L = [t.value]
    if t.children == []:
        return L
    else:
        for x in t.children:
            L = L + list_all(x)
        return L
 
def list_leaves(t):
    ''' (Tree) -> list
 
    Return a list of values in leaves of t.
 
    >>> t = Tree(0)
    >>> list_leaves(t)
    [0]
    >>> t = descendents_from_list(Tree(0), [1, 2, 3, 4, 5, 6, 7, 8], 3)
    >>> L = list_leaves(t)
    >>> L.sort() # so list is predictable to compare
    >>> L
    [3, 4, 5, 6, 7, 8]
    '''
 
    L = []
    if t.children == []:
        L.append(t.value)
        return L
    else:
        for x in t.children:
            L = L + list_leaves(x)
        return L
         
 
def list_interior(t):
    ''' (Tree) -> list
 
    Return a list of values in interior nodes of t.
 
    >>> t = Tree(0)
    >>> list_interior(t)
    []
    >>> t = descendents_from_list(Tree(0), [1, 2, 3, 4, 5, 6, 7, 8], 3)
    >>> L = list_interior(t)
    >>> L.sort()
    >>> L
    [0, 1, 2]
    '''
    L = []
    if t.children == []:
        return L
    else:
        L.append(t.value)
        for x in t.children:
            L = L + list_interior(x)
        return L
 
Notice that: in the list_all, I added the t.value immediately to the list I constructed, because we need to list all the values in t. 
 
However, for the function list_to_depth, I spend lot of time. Although I passed the test, I found a bug for the function. If put the >>> t = descendents_from_list(Tree(0), [1, 2, 3, 4, 5, 6, 7, 8], 2)
and >>> L = list_to_depth(t, 4)on the python shell, the out put is not right.
so I changed the body of the function and wrote a helper function.
 
def list_to_depth(t, n):
    ''' (Tree, int) -> list
 
    Return a list of values in t from nodes with paths no longer
    than n from root
 
    >>> t = Tree(0)
    >>> list_to_depth(t, 0)
    [0]
    >>> t = descendents_from_list(Tree(0), [1, 2, 3, 4, 5, 6, 7, 8], 3)
    >>> L = list_to_depth(t, 1)
    >>> L.sort()
    >>> L
    [0, 1, 2, 3]
    '''
    test = True
    L = []
    if t.value == n:
        L.append(t.value)
        return L
    else:
        if n in [x.value for x in t.children]:
            test = False
            L = [t.value] + [x.value for x in t.children]
            return L
        else:
            L = [t.value] + [list_to_depth(x,n) for x in t.children if test]
            return list1(L)
 
def list1(L):
    '''
    Return the one dimension list of the nested list L. 
    >>> L = [2, [3, [5, 6]], 7]
    >>> list1(L)
    [2, 3, 5, 6, 7]
    l = []
    for i in L:
        if isinstance(i, int):
            l.append(i)
        else:
            l = l + list1(i)
    return l
 
As above, the helper function list1 is a function convert a nested list into the one dimension list. I have not find another simpler way to solve the function list_ to_depth.
 
I will discuss it with my friends and ask TA after striking.
 
 
 
 
 


No comments:

Post a Comment