2015年2月23日星期一

Stack

    Stack is a kind of data type, which is last-in-first-out(LIFO) structure.  Basically, it can be achieved through two methods of list.  As the following, there are some codes demonstrating how it works.
    First of all, we need to initial a new stack.  In other word, we need to create a new empty list.
    Secondly, we can push any element into the stack through using a list method, list.append().

 
    Finally, if we need to delete the top element from stack, we can achieve by using list.pop() method.

    By and large, any elements push into the stack will be on the top of the stack, which will be removed firstly as well.  What's more, all of the operation can be achieved by two list method, list.pop() and list.append().

2015年2月15日星期日

Object-Oriented Programming

    Python is a kind of Oriented-Object Programming, which is a programming paradigm which is built on the concepts of class, inheritance, object.
    Class is a collection of the features, properties of an object.  For example, tree is a broad definition, containing all the features of tree, such as color, size and shape.
    Object is an example of class.  For instance, tree define all the characteristics of tree around the world, and Tina is a specific example tree, which has its unique shape of leaves and unique color of leaves.  The difference between class and object likes the relationship of the set of Z and "1" in math.
    Inheritance is a concept that there is a sub-class in a class.  For example, tree is a wide definition and may has its sub-class, such as pine tree and maple tree.  Tina may be a sort of pine tree which inherits properties and behaviors of its parent class and may have its own properties and behaviors.
    Message passing means that object implement a function through receiving a message or dealing message.  It likes a series of reactions when leaves fall.
    By and large, Oriented-Object Programming collects and save all the information and operations into the program.

2015年2月8日星期日

Recursive Function Exercise

What I wrote last is how to solve problems about recursive function.  In this week, I will write some exercises about recursive function.

count_elements(‘snork’)
count_elements([2, 1, 3])
The answer for the first one is 1, because ‘snork’ is not a list.  For the second one, the answer is 1.  Since [2, 1, 3] is a list, the function will run sum([count_elements(x) for x in L]) and will return sum([1 ,1, 1]) which is 3.


nd(‘ox’)
nd([])

The answer for the first one is 0, because ‘ox’ is not a list.  For the second one, since [] is a list though it is an empty list, the function will run 1 + max([nd(x) for x in (L+ [‘ox'])]) and L will become ['ox'].  Since 'ox' is not a list, which will let L become [0].  Then the function will return 1 eventually.
After doing these exercises, solving recursive function becomes much easier.

2015年2月1日星期日

Recursive Function

What professor taught to us this week was recursive function, which is brand new for me.  In the lecture, I could master the example that professor provided, sum_list function.  What we need to for the solution of this function just sums sublists and main list up.  However, when I did the exercise in the lab3 handout in the tutorial time, I just lost myself.  I did not know what I should do with that kind of problems.
         Therefore, I asked from help Ta and she just drew a graph and demonstrated to me how the function works steps by steps.  For example, find the solution for nm ([1, 3, 7, 5, 2]).

According to Ta’s method, firstly we need to determine L is a list or not.  In this question, L is a list, therefore, the function will return max([nm(x) for in [1, 3, 7, 5, 2]]).  Next step, let us consider the elements in the list L.  Since all of the elements in list (1, 3, 7, 5 and 2) are not a list.  Therefore, the function will return (1, 3, 7, 5 and 2).  Finally, the function will return max([1, 3, 7, 5, 2]), which is 7.  Thus the solution for this question is 7.

         This approach helps me a lot in solving recursive function.  Through this method, I can find out all the steps that the function works.