2015年2月22日星期日

Week 7 SLOG: Summary of Stack

In the lecture, I learned an abstract data type which is Stack. It is a last-in-first-out structure and there are some methods like pop, push, is_empty in its class.

First of all, in the Stack class, I need to initialize the stack to be empty.

class Stack:

def __init__(self):
    """
    (Stack) -> NoneType

    Initialize this Stack to be empty.






    >>> isinstance(Stack(), Stack)
    True
    """
    self.data = []

And then, I can create a method called push which can put object x on top of the Stack.

def push(self, x):
    """ 
    (Stack, object) -> NoneType

    Place object x on top of this Stack.
    """
    self.data.append(x)

Next, I can create a method called pop which can remove and return the top item in this Stack.

def pop(self):
    """
    (Stack) -> object

    Remove and return the top item from this Stack.

    >>> s = Stack()
    >>> s.push(1)
    >>> s.push(5)
    >>> s.pop()
    5
    """

    return self.data.pop()

Concluding two methods pop and push, it is easy to find this Stack is a last-in-first-out structure. Finally, I can create a method called is_empty to check whether the stack is empty.

def is_empty(self):
    """ 
    (Stack) -> bool

    Return whether this Stack is empty.

    >>> s = Stack()
    >>> s.push(1)
    >>> s.push(5)
    >>> s.pop()
    5
    >>> s.pop()
    1
    >>> s.is_empty()
    True
    """
    return self.data == []

Overall, if running this Stack, the object in this Stack will be put on the top of Stack and it is also removed out first. Therefore, it is an abstract data type with a last-in-first-out structure.

2015年2月15日星期日

Week 6: Summary of Object-Oriented Programming concepts

    In the Object-Oriented Programming, there are many concepts which are class, object, message passing, inheritance, encapsulation, polymorphism and abstraction.
    Firstly, class defines the abstract characteristics of a thing, which means that class defines general property of that thing and what it can do. And the methods in the class can be called the member of that class. Secondly, object means the specific things like some examples in the class. Thirdly, message passing indicates that an object achieves some functions by receiving message, processing message,passing out message or other methods. Fourthly, a class usually has some subclass which is more specific to embody himself and all subclass have the property that the class defines, which is the inheritance of the class. Fifthly, Object-Oriented Programming has encapsulation which means it hides a method of concrete steps, replaced by transmitting message through message passing. Encapsulation is only a particular class of objects can be accessed by members of the particular class restrictions and they often use interface inbound and outbound messages.Sixth, polymorphism indicates that different classes related by inheritance produced its target for the same massage will make different responses. Finally, abstraction is the way to simplify the complicated problem in the reality. It can find a suitable class for specific problems and the suitable subclass to explain problems.
    Overall, Object-Oriented Programming can be seen as the program that contains a variety of independent and co-ordination objects.
    


























2015年2月8日星期日

Week 5 SLOG for tracing Recursion

Again, here is some recursion exercises I have done before:

def count_lists(L):
    '''(list or non-list) -> int

    Return 0 if L is a non-list, or number of lists in possibly-nested L.
    
    if isinstance(L, list):
        return 1 + sum([count_lists(x) for x in L])
    else: # L is not a list
        return 0

1)Trace([2, 1, 3])
Answer: 1

Because there is only one list which is himself, and the elements in the list are non-list. Therefore, it returns (1 + sum[0, 0, 0]) which is 1.

2)Trace([2, 1, 3, '[4]'])
Answer: 1

'[4]' is not a list so it returns (1 + sum[0, 0, 0, 0]) which is 1, like above one.

3)Trace([7, [2, 1, [3, 4], 5], 9, [3, 2, 4]])
Answer: 4 

Because there are 4 lists, it returns (1 + 1 + 1 + 1 + sum[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) which is 4.


Here is the other one example:

def count_all(L):
    '''(list or non-list) -> int

    Return 0 if L is a non-list, or number of lists, plus non-list elements,
    in possibly-nested L.
    
    if isinstance(L, list):
        return 1 + sum([count_all(x) for x in L])
    else: # L is not a list
        return 1

1)Trace([2, 1, 3])
Answer: 4

According to the code, I know that if L is a list then it returns 1 + sum(elements in L), and if the element in L is also a list then it will run again like 1 + sum(a in element).If the element is not a list, then it returns 1.

So [2, 1, 3] is only one list which is himself. Then it returns 1 + sum([1, 1, 1]) which is 4.


2)Trace([2, 1, 3, [4]])
Answer: 6

[2, 1, 3, [4]] has two lists, then it returns 1 + 1 + sum([1, 1, 1, 1]) which is 6

3)Trace([7, [2, 1, [3, 4], 5], 9, [3, 2, 4]])
Answer: 14 

[7, [2, 1, [3, 4], 5], 9, [3, 2, 4]] has 4 lists, then it returns 1 + 1 + 1 + 1 + sum([1, 1, 1, 1, 1, 1, 1, 1, 1, 1]) which is 14.


After some exercises, I find it is easy to trace recursion if I understand the codes and I know how it works. :)



2015年2月1日星期日

Week 4 SLOG about Recursion

In this week, recursion is a new concept for me, and I think this is hard to understand how it works at first. I usually get wrong way in some problems.But I work it out with some help of TA and I try some exercise in the lab successfully.
    
For example, in this question:

 def   count_elements(L): 
        ’’’(list or non-list) -> int 

        Return 1 if L is a non-list, or number of non-list elements in possibly- nested list L. 

       Assume: L is any Python object. # examples omitted! 
       ’’’ 

       if isinstance(L, list): 
           return sum([count_elements(x) for x in L]) 
       else: # if L is not a list, count it 
           return 1

1.Trace count_elements('smoke')
 The answer is 1 because 'smoke' is a string but not a list.

2.Trace count_elements([2, 1, 3])
 The answer is 3 because [2, 1, 3] is a list and then the elements in the list  are 2, 1, 3 which are integers but not lists. Then it comes out sum[1, 1, 1]  which is 3.

3.Trace count_elements([[2, 1, 3, '[4]’]])
 The answer is 4. In the same way, it goes through the loop again and again and  it comes out sum[1, 1, 1, 1] which is 4.

4.Trace count_elements([7, [2, 1, 3], 9, [3, 2, 4]])
 The answer is 8 comes from sum[1. 1. 1. 1. 1. 1. 1. 1]


Overall, solving the recursion problem is going through the loop. And the best way to work it out is write down all things step by step.