2015年4月2日星期四

Week12: Last entry-- A Look Back

First, I'm so glad that the strike was over. Also, the semester is coming to an end. Throughout the semester, the content of CSC148 is more class-based, whereas CSC108 teaches far more about how to write out codes and debug. Therefore, I would say CSC148 is more conceptual.

Look back all my posts, I wrote about Object-Oriented-Programmings most. Since python's world is all about objects and classes, such as Trees, Linked-lists, BST and so on. When I was learning CSC108, classes were confusing me. During the course of learning CSC148, I have found myself  getting my way into OOP. Now I even have started to like it.

This is a interesting course for me. At the beginning I decided to minor in computer science but after this course, I have changed my mind. Now I'm pursing a double major in computer science and statistics. Due to this course, I've started to love doing labs, which I hated most in high school. Coming labs every week helps me think about and practice the materials learned in lectures weekly, since during lectures I can hardly think independently. However, during labs with TA's help, discussions and multiple corrections, I was able to absorb materials.

Thanks to Danny, Diane, and my lovely TA. I had a wonderful experience and feel ready to study higher level computer science courses in the following years. :)

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.

2015年2月13日星期五

Week 6: Object-Oriented Programming

Based on my understanding, Object-Oriented Programming(OOP) is a programming language model organized around objects. It's all about manipulating objects.

Here's a helpful video I found:

In this video, the teller introduces OOP concepts by explaining some terms first, such as 'module', 'class' and 'object'. 

He prefers to think a module as a collection of classes and a class as a collection of objects. 

To help understand these abstract terms, he defines a 'class' as a blueprint of what you want to create, for example, a buleprint of two-room house. Based on this blueprint, we can bulid houses- concrete froms, instantiation of the class- called as 'object'.  To construct other parts of the city, we need  houses in different structures(two-room, three-room and so on).  A 'module' is a collection of the blueprints of houses in different structures.

  

Since these blueprints are similiar, to make it easier to create new houses, we can copy a part of attributes from the original blueprint, say a two-room blueprint. With the property of inhertance, two-room buleprint can be extended to three-room blueprint by adding one more room. In this example, the two-room bluprint is called 'base class' and the three-room blueprint is called 'derived class'.  

2015年2月8日星期日

Week 5: Tracing Recursion

RECURSION is squeezing my head this week...

I got the picture from:
http://darkfoxnewslog.blogspot.ca/

Clearly, I'm not the only one who is suffering from recursion. Luckily we won't be tested on writing recursion codes on Wednesday. I was holding that thought until seeing the last year test. I got wrong on the first question which was to trace a recursion.



Now I have concluded a standard thought to such question, take that question as an example:

Just trace s(1), s(2)...by pencil as carefully as you can!


s(1): (1, 1) since n == 1


s(2): min([2*s(1)[0] + 2*1 -1, 1]) == min([3, 1])


s(3): min([2*s[3-2][0] + 2*2-1, 2], [3, 1]) == min([5, 2], [3, 1])

and so on







Note: be careful of over-tracing

2015年2月3日星期二

Week 4 : Think like a computer scientist (recursion)

My impression of first few weeks is that in order to think like a computer scientist you have to concrete the abstract concepts in  your mind. When doing the first two labs, I had trouble about understanding. For example, how to write methods as instructions indicate, what are subclass and how to implement it.

After doing all additional exercises, I kind of got some pictures in my head while coding. For example, when writing class Roster, I imaged myself create a blank sheet while writing r.Roster() and draw a line in the middle of the sheet while writing self.female = {}, self.male = {}. Likewise, every time I read the description of the class, I applied my imagination.

When it comes to recursion, the imagination lost its power. I had to trace step by step to help me understand the purpose of the method(e.g.count the number of lists)I have started to the concept of recursion,  “defining something in terms of itself multiple times”. Here is an example with comments are my understanding of each line:

    def count_elements(L):
    '''(list or non-list) -> int
    Count all elements in a given list(may contains many inner lists).
    '''
    if isinstance(L, list):
        return sum([count_elements(x) for x in L]) # if list, deg deeper
    else:
        return 1 # count plus 1 until the element is not list

Hope I can use recursion correctly and quickly through more practices.
   

2015年1月24日星期六

Week 3: Why Geeks Should Know How To Write

When talking about 'Geeks', Many people will come up with a view of a sluggish man indulging in his own world working alone under a basement. However, the word to me referring more about one's expertise. To have a project done, geeks need to work in team and cooperate, just like our group assignments. Beside from distributing jobs, the most important thing is communication so that we can understand each other's codes and ideas. Now that requires efficient and clear writing style.

Joel Spolsky says 'I won't hire a programmer unless they can write' in his article 'Advice for Computer Science College Students'. In this article, he particularly talks about the importance of writing and why he thinks so. In summary, programmers who speak or write well have advantages when they: tell the non-professional people about new-tech products, persuade other people, and communicate with other programmers. I strongly agree with the last point. There are many times  programmers working together. With clear comments and technical spec, other programmers can understand the codes and use the codes instead of rewriting them. Good writing skills can prevent brilliant codes from being wasted.

Different from 'novel-kind-of writing', a good programmers should be able to write in an organized, clear and readable style. So the geeks should know how to write but instead of creativity, we are looking for more about efficiency while writing.

As one of the geeks, the more assignments I do, the more I like teamwork. I think that's because of the proper communication and record: always writing down ideas. :)