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.