Monday, March 30, 2015

Slog for week11: revisit previous posts

For this week's slog, we are required to revisit earlier posts that we wrote ourselves, and see whether I agree or disagree with our own opinions at past, then compare it to our classmates.

As I revisit my own slog, there was some posts I agree but some that I disagree too.

At first few weeks' slogs, I wrote I am confusing about str and repr method, and I did not clearly
write down their differences, but now I can confidently say that their differences: while __str__ method is used for printing out the state, __repr__ method returns a string representation of state.

For example,
__repr__ method is used as
       >>> s = SubtractSquareState('p1', current_total = 17)
        >>> s
        SubtractSquareState('p1', False, 17)
while __str__ method is used like
       >>> s = SubtractSquareState('p1', current_total=17)
        >>> print(s)
        Current total: 17; next player: p1

The big difference between them is when we simply write s, it returns string representation by __repr__method, but when we print s, it returns string representation by __str__method.

Other than this, I mostly agree with previous posts.
(The way I explain how to approach to recursive functions, the explanation of btnode, etc)

As I visited several classmates' slogs and compared what I and they have similar difficulties,
I realized that most of my classmates experienced the difficulties on LinkedLists, and LinkedList question on term test 2's just like me. And mostly almost all classmates feel uncomfortable about strike we have, because of cancellation of labs. This point is what I mostly agreed with, since I really have difficulty to finish lab myself.

I strongly impressed with this classmate's slog. (http://dottoripy.blogspot.ca/)
He writes down every week's slog with details including numerous codes we did on lecture, and adds explanations of terms from other sources.(for example, Object Oriented Programming on week 6) I thought I would be better if i also add definitions or principals like him, to clarify the terms.

Slog for week10: term test 2 and short lecture about binary tree

Since the strike has been continued, there was no tutorials on this week as last week. 
On last week, week 9, we had term test 2 and 35 minutes of lecture, and it was about two new methods of binary search tree, 'insert' and 'delete'.

The complete version of function insert is 


def insert(node, data):
    ''' (BTNode, object) -> BTNode

    Insert data in BST rooted at node if necessary, and return new root.

    >>> b = BTNode(8)
    >>> b = insert(b, 4)
    >>> b = insert(b, 2)
    >>> b = insert(b, 6)
    >>> b = insert(b, 12)
    >>> b = insert(b, 14)
    >>> b = insert(b, 10)
    >>> print(b)
            14
        12
            10
    8
            6
        4
            2
    <BLANKLINE>
    '''
    return_node = node
    if not node:
        return_node = BTNode(data)
    elif data < node.data:
        node.left = insert(node.left, data)
    elif data > node.data:
        node.right = insert(node.right, data)
    else:  # nothing to do
        pass
    return return_node


As we can notice from the above, the function 'insert' is basically creating 'a new binary tree,' then insert data in corresponding position (this is based on the size of number). 
so if nothing in tree yet, we just add data -> return_node = BTNode(data)
if data is existed, and new data is bigger than datas in node ->
 node.right = insert(node.right, data). 

The importance of this code is 'recursion'.
On both data < node.data and data > node.data, we should use recursive function, at this point, 'insert', thus keep running until new data(which is we just add) puts on the right position. 


Another method, 'Delete' was almost similar to 'insert' method, except two more cases we should consider. 



Since I am quite familiar to binary tree because of preparation of term test 2, week 9's lecture was quite simple, and understandable.

The one thing I am worrying about is lab because it is hard to review and study lab every week without tutorials. I need TA's help during tutorials about lab since the solutions of labs are not posted until few days before term tests. 
Piazza does not help that much as TA's. I want quick response, quick help from TAs about lab.

I hope strike will be over on next week! 

Sunday, March 15, 2015

Slog for week9: term test 2

On last week, we had term test 2, which was held on during the first half of regular lecture time.
On term test 2, there were three questions, and I believe I did not do very good.
The first question was about the basic concepts of tree class, drawing trees with the height of 3 and arity of 3 and list the tree by postorder. Because I only wrote down and memorized inorder _visit, I had problems about solving postorder question. I re-visted the previous lab, which contains preorder_visit code, and reviewed it about the test for preparing the finals.

def postorder_visit(root, perform):
    ''' (BTNode, function) -> NoneType

    Visit each node of binary tree rooted at root in postorder
    and perform.

    >>> b = BTNode(8)
    >>> b = insert(b, 4)
    >>> b = insert(b, 2)
    >>> b = insert(b, 6)
    >>> b = insert(b, 12)
    >>> b = insert(b, 14)
    >>> b = insert(b, 10)
    >>> def f(node): print(node.data)
    >>> postorder_visit(b, f)
    2
    6
    4
    10
    14
    12
    8
    '''
    if root is None:
        pass
    else:
        postorder_visit(root.left, perform)
        postorder_visit(root.right, perform)
        perform(root)


The second question was pretty simple since we had the similar method on previous lab.
def list_between(node, start, end):
    ''' (BTNode, object, object) -> list

    Return a Python list of all values in the binary search tree
    rooted at node that are between start and end (inclusive).

    >>> list_between(None, 3, 13)
    []
    >>> b = BTNode(8)
    >>> b = insert(b, 4)
    >>> b = insert(b, 2)
    >>> b = insert(b, 6)
    >>> b = insert(b, 12)
    >>> b = insert(b, 14)
    >>> b = insert(b, 10)
    >>> list_between(b, 2, 3)
    [2]
    >>> L = list_between(b, 3, 11)
    >>> L.sort()
    >>> L
    [4, 6, 8, 10]
    '''
    if node is None:
        return []
    else:
        left_list = (list_between(node.left, start, end) 
                     if node.data > start 
                     else [])
        right_list = (list_between(node.right, start, end) 
                      if node.data < end 
                      else [])
        node_list = ([node.data] 
                     if (start <= node.data <= end) 
                     else [])
        return left_list + node_list + right_list


The question 2 was almost same as the above code, but it has little difference (I do not remember exactly, but I added more code on node_list.


The difficulty of this term test was the third question: I totally had no idea how to insert the new node that is half of previous node.back.value. I am going to review about LLNode and LinkedList after the sample solution is posted.

I hope the strike will end soon. I have a lot of questions and need help from the lab since piazza is not enough for me. Until the strike ends, I will use office hour and piazza and also self-study more using the sample solution of labs.

Thursday, March 5, 2015

slog for week 8: impressions for week 7

Topic for Week 7 --> "Binary Tree"

We had two hours of lecture about how to represent binary trees in Python. Since the writing class for "binary tree" is similar to stack, class (which we covered until now during lectures), "binary tree" should not too challenging to me. 

Here is the code given for the lecture, as we can see from the body of __init__ function  


    def __init__(self: 'BTNode', data: object,
                 left: 'BTNode'=None, right: 'BTNode'=None) -> None:
        """Create BT node with data and children left and right."""
        self.data, self.left, self.right = data, left, right

we can realize that this is what we have seen during last several lectures(which means it's not challenging). 
However the difference is also found here: the code is written in just one line, different from what we had before.

I think the nicest about binary trees is that it is a great example for doing recursion since a parent tree can have several children(sometimes no child) and then keep continuing. If we know something about its subtrees, let's say leaves, then we know the number of leaves of the whole binary trees.


    def __repr__(self):
        ''' (BTNode) -> str

        Represent BTNode (self) as a string that can be evaluated to
        produce an equivalent BTNode.

        >>> BTNode(1, BTNode(2), BTNode(3))
        BTNode(1, BTNode(2, None, None), BTNode(3, None, None))
        '''
        return 'BTNode({}, {}, {})'.format(repr(self.data),
                                           repr(self.left),
                                           repr(self.right))

In __repr__ method, repr, itself is used three times in the body lines (this is called as recursion).

The professor, heap covered three examples of how to order binary trees (preorder, inorder, and postorder). All of three functions involve recursion, and I find it really helpful to run the recursive functions in Python visualizer. It can help you understand recursion better.

Also, heap talked about Tree class, the most interesting of this class is basically all of its method involve recursion. So when he wrote the method, I experienced a hard to follow him. (also one-line-coding was difficult)  But I did review all the methods at home and looked again the lab which is related to "binary tree". For the method called, 'descendents_from_list,' the last three lines were confusing even though I wrote down the examples step by step followed by the body of the code. I visited help center in BA, to get some help, and now I feel confident for all methods in btnode.


def descendents_from_list(t, L, arity):
    ''' (Tree, list, int) -> Tree

    Populate t's descendents from L, filling them
    in in level order, with up to arity children per node.
    Then return t.

    >>> descendents_from_list(Tree(0), [1, 2, 3, 4], 2)
    Tree(0, [Tree(1, [Tree(3), Tree(4)]), Tree(2)])
    '''
    q = Queue()
    q.enqueue(t)
    L = L.copy()
    while not q.is_empty(): # unlikely to happen
        new_t = q.dequeue()
        for i in range(0,arity):
            if len(L) == 0:
                return t # our work here is done
            else:
                new_t_child = Tree(L.pop(0))
                new_t.children.append(new_t_child)
                q.enqueue(new_t_child) 
return t