Wednesday, April 1, 2015

Slog for last lecture on last week!

Finally every CSC148 lecture in winter session is over;
It was great course with professor Danny's passion and humor during the semester.
Although the strike had been continued for almost two months, still CSC148 was fine.

As I looked back the whole semester, we had two term tests, three assignments, and six quizzes.
Until the strike begun, I was doing great with pretty high grades of term test and the first assignments, and some quizzes. But I was getting lazy and have not much passion as before after labs were cancelled.

Before the strike begun, I went every tutorials because it was mandatory; During each lab, I questioned a lot to TA and classmates that I was not able to understand the concept by myself. Therefore I am confident of creating class and recursive function. Without labs, I became lazy to preview and review the labs because I could not get the help as easy as before. I should use office hours but mostly it was not fit to my timetable, and piazza does not reply me back so fast.

As a result, my term test 2 mark that I just received from today, the last lecture, was not good.
I think cancellation of labs affects a lot to this semester's students. Without labs, I could not perform well on computer science course.

Except the strike, the other things were fine. The content of lectures and labs, the way Professor Danny taught us, and assignments help me to follow the flow of the course. Every definition and concepts of content was well-explained during the lecture slide, so that the only thing I need to do is actual practice of coding with numerous trials. I think this is the only way I can do better for the final and other computer courses for the future.

CSC148 was the great course that makes me to figure out how I should study for computer science.

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

Thursday, February 26, 2015

Slog 5: recursion

At first time, I found the concept of recursion very confusing, but this example(from lecture) helped me to apprehend it.


def sum_list(L: list) -> float:    """    Return sum of the numbers in L.
    L --- a possibly nested list of numbers.  L, and all sublists, are          non-empty
    >>> sum_list([1, 2, 3])    6    >>> sum_list([1, [2, 3, [4]], 5])    15    """
    return sum([sum_list(x) if isinstance(x, list) else x for x in L])

and this code for the same function:

def sum_list(L: list) -> float: 
    total = 0
    for x in L:
        if isinstance(x,list):
            total = sum([total, sum_list(x)])
        else:
            total = sum([total,x])
    return total
 
The seemingly complicated problem of how to solve a list of lists is easily solved with recursion. 
To sum a list of lists, start with the initial list, if the first index of that list is a number, at it to the sum, if the next object at the next index is also a number, at it to the sum too, and so on. It is easy to write a function to do this, it's just getting the sum of numbers in a list. When a nested list is encountered, we still want to get the sum of the numbers in a list, however our object is a list instead of a digit. So, we just call our function on this list, then we get the sum of the nested list and it is added to our total sum. The big problem is solved by repeating the solution to a sub problem, in this case, summing lists.


The recursion handout, which is from the lecture, and the lab about recursion helped me out. Especially during the lab, the lab worksheet contains the way to tracing each steps of recursion function, so that I was able to understand step by step. Not only from the lecture and lab, but also from piazza, I learned some confusing concepts. For example, I was confused about writing each steps of inner recursive part, but viewing other classmates' comments and posts, I solved my difficulties.

Wednesday, February 11, 2015

Slog for Week6: summary of Object-Oriented Programming concepts

Before I begin to write summary, what is "Object-Oriented Programming concepts?"
"Object-Oriented Programming" is a programming paradigm that views a computer program as a combination of data structure.

So what we learned in csc148 about "Object-Oriented Programming concepts?"
We learned about formatting parent class with several methods such as __str__, __eq__, __init__, and etc, and formatting subclasses based on its super class.

Basically when we write the super class, sometimes several methods in super class are written as 'NotImplentedError'. This is because super class does not specify only one specific subclass, but it is a 'general class' that contains several subclasses' general  information at one time.

For the subclasses, now we write the code that what we need to implement. If the method is already existed in its super class, and it does not need to extend, then you can simply skip it. However when you feel it needs some extension, then you write the code, and sometimes a new method for only itself.

Designing class and subclasses is not hard;
First before you start to write the code, draw a big picture how you are going to write.
then think what code you will need, and the appropriate names for each method and classes.
If you can do these two things, then you will be safe in "object-oriented programming."

I also feel difficulties designing classes at first time, but the practice helped me a lot.
Assignment 1 really helped me, and first two labs too.