2015年3月27日星期五

Week11: Revisit OOP

Thanks for the blog I commented  :http://computersciencemaggie.blogspot.ca/2015/03/summary-of-object-oriented-programming.html?showComment=1428022607229
In my earlier post, I only discussed about the concepts of class and inheritance. In ComputerScience0909's blog, the author not only summarized in a more efficient and clear style, also discussed other Object-Oriented Programming concepts, such as Recursion, Trees, and AbstractDataTypes. Comparing to his ideas, I have found my explanations are not specific enough.

To sum up my thoughts so far, python's coding style is to class things up. Since my partner works part-time  in the mobile gaming area, he has found out that they implement games by a 'Model, View, Controller' approach, which is similar to our Game_State class and Strategy class. I'm very lucky to have a partner who is enthusiastic in computer science. I have learnt a lot form him. :)

Also, this week we revisited sorting selections to study the efficiency of sorting. To understand the how these selections work clearly, the illustrations are needed for me. The following blogs summaries the selections clearly with good images.
http://darkfoxnewslog.blogspot.ca/2015/03/slog-for-week-11.html?m=1









Bubble sort






















I'm feeling myself thinking more like a computer scientist!

2015年3月21日星期六

Week10: Assertion and Unit Tests

This week we explored assertions, unit tests, and tracing function calls.
When you do

assert a_condition
you're telling the program to test that condition and trigger an error if the condition is false.
Syntax: assert  « condition » ,  « error_string »
Meaning: If the condition fails,
        raise AssertionError( « error_string »)
I don't really understand why we should use assertions. Although the handout said that assertions can help us avoid 'nosiy' output and save the time of testing, I didn't really see any examples to prove the efficiency of using assertions.Since the strike is on, I have no one to ask.

I found a website that provides some examples:
https://wiki.python.org/moin/UsingAssertionsEffectively



2015年3月13日星期五

Week 9: modify BST instance


Due to the test, we only had one class this week. In the lecture, we explored BST methods which may modify the BST instance they are called on. Furthermore, mutations and iterations are not only related to BST, but to LinkList as well.

There is a general format for iterations in a LinkedList which I copied from the slide:

cur_node = self.front
while : # do something here... 
cur_node = cur_node.nxt #walk along 



After all, the test was easier than i thought and cheating sheet was really helpful for the last question. I only changed couple lines of __delete__ method.

def delete_after(lnk, value):
    ''' (LinkedList, object) -> NoneType

    Delete node with value after the first occurrence of a
    node containing value, if possible.

    >>> lnk = LinkedList()
    >>> lnk.append(3)
    >>> lnk.append(5)
    >>> lnk.append(7)
    >>> lnk.append(9)
    >>> delete_after(lnk, 3)
    >>> print(lnk)
    3 -> 7 -> 9 ->|
    >>> delete_after(lnk, 7)
    >>> print(lnk)
    3 -> 7 ->|
    >>> delete_after(lnk, 15)
    >>> print(lnk)
    3 -> 7 ->|
    '''
    if lnk.front:
        cur_node = lnk.front
        # walk along the list
        while cur_node and not cur_node.value == value:
            cur_node = cur_node.nxt  #walk along the list
        if cur_node and cur_node.nxt:
            if lnk.back is cur_node.nxt:#if the node needed to be deleted is back
                lnk.back = cur_node#just change the value of back to cur_node
            cur_node.nxt = cur_node.nxt.nxt#if not the last node, delete cur_node.nxt 
            lnk.size -= 1#always remember to change the size of a modified link

However, I forgot to draw a diagram for the last question....sadly:(

2015年3月6日星期五

Week 8:Linked node and Linked list

After attending two lectures, still, I didn't understand what a linked list really is. My way to understand a OOP concept is to generate a diagram in my head when implementing functions or initializing the object. Unfortunately, no pictures ever appeared when I saw Danny's codes for LLNode and LinkedList until I read the reading Danny posted: http://openbookproject.net/thinkcs/python/english3e/linked_lists.html



Generally, a LinkedList(formed by LLNodes) has three variables: front(represents the first node), back(refers to the last node), and size(stands for the number of the nodes in the LinkedList). 

Now while implementing methods I can understand what each line for. 
For example:

def insert_before(lnk, v1, v2):
    ''' (LinkedList, object) -> NoneType

    Insert a new node with value v1 before the first occurrence
    of a node with value v2.  Do nothing if no node has value
    v2.

    >>> lnk = LinkedList()
    >>> lnk.prepend(5)
    >>> insert_before(lnk, 4, 5)
    >>> print(lnk.front)
    4 -> 5 ->|
    >>> insert_before(lnk, 3, 5)
    >>> print(lnk.front)
    4 -> 3 -> 5 ->|
    >>> insert_before(lnk, 3, 7)
    >>> print(lnk)
    4 -> 3 -> 5 ->|
    '''
    if lnk.size > 0:
        prev_node = None   #always before cur_node
        cur_node = lnk.front
        while cur_node and not cur_node.value == v2: #if not None not v2, keep walking along the list
            prev_node = cur_node
            cur_node = cur_node.nxt
        if cur_node and cur_node.value == v2: #if cur_node is v2
            cur_node = LLNode(v1, cur_node)
            if prev_node is None: #if the cur_node is the first
                lnk.front = cur_node
            else:
                prev_node.nxt = cur_node
        lnk.size += 1#always remember to change the size of a modified link 

This week I prepared for the second term test by reviewing labs and doing couple danny's past exams. Good luck!

2015年3月1日星期日

Week7: OOP and Tree Class

Please grade my previous post as my chosen topic is OOP:
http://ruolancsc148.blogspot.ca/2015/02/week-6-object-oriented-programming.html

Week 7 – continued

This week we continued on the topic of object oriented programming – Tree Class. We continued implementing recursive functions, but unlike the previous week, the input was a BTNode , the root of a binary tree.

After initializing a BTNode, we constructed different functions by using the concept of recursion. During the lab I've found drawing diagrams is super helpful for clearing thoughts. I couldn't figure out many functions until drew a diagram. For example, the last function list_longest_path:
def list_longest_path(t: BTNode):
   ''' List the data in a longest path of t.'''
    L = []
    if not t:
        pass
    else:
        L = [t.data] #collect the value of the root of the tree
        if len(list_longest_path(t.left)) >= len(list_longest_path(t.right)):#collect only left path if it is longer
            L += list_longest_path(t.left)
        if len(list_longest_path(t.left)) < len(list_longest_path(t.right)): #collect right  path if it is longer
            L += list_longest_path(t.right)

Based on my understanding I kind-of summarized a recursive algorithm for collecting data:
1. Create an empty list or dictionary;
2. Separate the base case (which is usually very simple) and other cases that need recursive concept;
3. Set the requirements for data to be collected.

All we need are a base case and a way of proving that for any list, we will eventually get to the base case.

Also, I would like to thank Julian's post(on which I commented):


It helped me to clarify and summarize every concept Diane mentioned this week.