Sunday, 15 February 2015

week 6: Tree Class( The summary of Object_Oriented Programming concepts

This week, we have learned how the Tree Class works and how to write some recursive function based on Tree and Queue.

Actually, I did not know the function descendents_from_list before lab this week. I'm so thankful my Ta of our lab, he told me that descendents_from_list function is a function to build a tree based on the arity. For example, the tree descendents_from_list(Tree(0), [1, 2, 3, 4], 2) is a tree starting with Tree(0) and listing the 2 nodes which is the same number as arity from the first two elements of the list; thereby, the 2 nodes will have the same number of children as arity if there were enough elements remained in the list. In the situation of descendents_from_list(Tree(0), [1, 2, 3, 4], 2), the tree starts with 0, two nodes tree 1 and tree 2, and tree 1 has two nodes 3 and 4.

Furthermore, in the lab exercise, I learned how to write a recursive function more deeply compared with last week. I know when the function called itself, it would start a brand new id location to store his value used in function body, and we need to 'building a place' to store the total useful value when we ran it.

I'm expecting next lecture! By the way, I feel recursion is very interesting and will be very useful in the further study.

Sunday, 8 February 2015

Week 5: the stack and queue.

This week we learned the class Stack and class Queue. These two classes actually are two way to store and remove elements of list or string.

For class Stack: we store things like filling something into a bottle, so the element added last was pulled out first. In other words, remove the element at the end of the list or string.

For class Queue: we store things like lining up a line so that the element added first was pulled first. In other words, remove the element at the beginning of the list or string.

In Lab#4, we have the exercise with the Stack and Queue. I think the hard part is storing the list in right arrangement so that the output of methods matches the requirements.Firstly, I used recursion function I learned last week to transform the nested list ([1, [2, 3], 4]) in to a simple list ([1, 2, 3, 4]). Then, we can use the simple list to be stored by Stack.push(). Similarly, we also need change the format of the input when we use the class queue. Secondly, we use input('Type...:') and while loop with __.is empty for both class to store the string and integer for class Stack and class Queue respectively.

I feel these two classes will be very useful in the future, let's see!

Sunday, 1 February 2015

Week 4: My Impression of Week 4

After a whole weekdays, we had studied so much new knowledge about Python. 

Firstly, I learned the differences between __repr__ and __str__.  __repr__ need to be implemented for any class you implement, sometimes working like 'self' input. Implement __str__ if you think it would be useful to have a string version which give the users information about errors or subclasses and so on. For example, the class named GameView(): then if __repr__ defined to be return GameView(), the input g.__repr__() on shell is a input. However, __str__ will give you information like : 'Requiring a subclass' provided that def __str__(self): return ('Requiring a subclass').

Secondly,  we studied a very important thinking in computer science -- recursion. Recursive thinking is a way that call the function itself to solve a complex and long problems. People maybe wanna ask why we should learn a hard way to code, the answer is that recursion can solve questions in elegant and concise way. On the week 4 lab, we traced a recursive function and write out an unknown doc-string of another recursive function. Nevertheless, the most challenging part is write a recursive function based on the doc_string and examples. I coded that if isinstance(L, list): return max([len(L), length(x) for x in L])   else: return 0. Actually, this function spend me 20 minutes, since it used len() and max() two existing functions so that hard to combine 2 functions. 

Anyway, it is a long way to learn recursion. Studying hard and get good mark.