2015년 2월 15일 일요일

Week 6: Summary of Object-Oriented Programming Concepts

      This week, we have learned about Object-Oriented Programming. From what I have understood, this works sort of like a family tree. Here are some important terminologies that were useful when doing programming. Node is the parent of the tree which can be distinguished as a root. Then there are the children below Node. Internal node is a node with one or more children. Leaf is a node with no children. Arity states a maximum number of children a node can have.

This is a general tree implementation from the lecture slide:

class Tree: 
      ’’’ Represent a Bare-bones Tree ADT’’’
      def __init__(self, value=None, children=None): 
            ’’’ (Tree, object, list) -> NoneType

            Create Tree(self) with root node value
            and 0 or more children. 

            ’’’ 
            self.value = value 
            # copy children if not None
            self.children = children.copy() else []
            # the functional if on the line above is equivalent to :
            # if not children:
                  #self.children = []
            #else:
                  #self.children = children.copy() 
   
From this class Tree, we can find the height of the tree, find how many leaves, and the arity of the tree T by using def---.

For me, counting leaves were a bit confusing. But I found a way of doing it. If there were a method:

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]
    '''
I would first look at the arity, which in this case is 3, next to the eight numbers in a list. This means on the top of the list below 0, there will be three numbers. Therefore it'll look like:

0
1          2          3
Then below each '1', '2', and '3', there also will be 3 children.

0
1          2          3
4 5 6     7 8              
Therefore, [3,4,5,6,7,8] will be the leaf because they do not have any children. 

2015년 2월 7일 토요일

Week 5: Impressions of tracing recursion from week 4

On week 4, we learned how to trace recursions. We also got a worksheet in class to practice tracing recursions. From the work sheet, recursions are usually written in this form:

def c(s):
     """ (Docstring omitted)
     """

     return sum ([c(i) for i in s]) if isinstance (s, list) else 1

Which means if there is a list, for example c(["one", 2, 3.5], the isinstance (s, list) will check if s is in a list. If so, the function will use a for loop to go over each element in s with an "i". However, I noticed that in this case, the "i" will be put in a c(i) for every element and in the end, all the elements in the list will refer to the else part of the fuction where in this case, it will return 1. Then the function will find the sum according to how many 1's there will be. I noticed that the faster way to figure out the answer in this case is to count how many elements there are in a list and times it by the number that is in the else part.

Another example of a recursion is:

def sum_list(L):
     """ (Docstring omitted)
     """

     if isinstance (L, list):
          return sum([sum_list(x) for x in L])
     else:
          return L

In this case, it is similar to the example above. However, in the return section, it says return sum([sum_list(x) for x in L]). So when the for loop is running, x will go over each elements in the list L and will be put in a sum_list(x). If this were to be saved as (a, b, c), then in the end, the y will be written sum([a, b, c]) which will produce a+b+c.
Example: sum_list ([4, 1, 8]) ---> sum([sum_list(4), sum_list(1), sum_list(8)])
                                           ---> sum([4, 1, 8])
                                           ---> 13