Sunday, March 29, 2015

week11: Revisit my earlier SLOG and visit my friend's slog

In this week's slog, I will revisit my earlier SLOG and check my earlier ideas.


In my week4's slog, I wrote:"self was very trick for me. But I decided not to understand but to remember it! For every method in a class, remember writting self!!!" However, now I understand that "self" is just the class itself. So when we call method in a class, "self" tells system which class is used.


In week7's slog, I wrote my point of view for benefits of Object-Oriented Programming. Now after finishing my assignment 1&2&3, I get another point. Using class is quite convenient to add something new to your code! In assignment 1, we wrote "subtract_square_state.py" and in assignment 2 we wanted to write another game state called "tippy_game_state.py". By using class we can keep "subtract_square_state" in place and wrote another class inherited from super class "game_state".



(Here is the result in python shell after running "Game_view". By using class we can choose the game which we want and choose strategy.)


In my view, it is efficient if the program is huge.I also find that it is efficient for "debug" because we can choose different games to check our code as we want.

Another point which is worth mentioning about OOP is that I find our code  is much easier to understand by using class.
For example:


In class Queue, it is quite clear that this data type is something like a list and we can add data using method enqueue. Using class is much easier for us to understand the code than just providing a branch of functions.


I also visited my friend's SLOG this week. What is worth mentioning is my partner Celina's slog!




I found something useful in her slog. In week 7, she gave a summary for abstract data types. After giving a summary for each type, she provided a website link such as:
"http://interactivepython.org/runestone/static/pythonds/BasicDS/TheStackAbstractDataType.html"
I visited those website and found it is quite a good summary for abstract data types!

It gives a summary for each data type and give an example for each method!

I have highlighted this website! Thank you Celina~!


Sunday, March 22, 2015

week10 my impression on week 9

In week 9,we took term test 2. In lecture we talked about how to insert and delete elements in a binary search tree.

In test, the hard part for me was a question called "visit in order". In fact I didn't hear about it before the exam and I didn't have a expression about we talked about it in class.

After the exam, I talked about traversal with my friends but they couldn't give me a clear answer too! So I searched it using google and I found the answer.

The tree pictures below represent traversal in-order,pre-order, post-order and level-order quite clearly.

 
 

In week 9, we only had one-hour lecture, and we talked about how to insert and delete elements in a binary search tree.

To be honest, the "delete" method bothered me until now.I didn't get the point about how it works.

The code for "delete" is at the end of the slog.

Firstly,why shall we write "elif not node.right" ? Can we just write "return_node == node"?

Secondly, what dose it mean by "node.data = find_max(node.left).data"?


To solve my problems, I decide to attend next week's office hour.

Sunday, March 15, 2015

week9 :my impressions of week 8( LLnode and LinkedList)

This week we talked about LinkedList and LLnode.

LLnode is just a single node.

A linked list is a linear sequence of nodes. Each node has a value and a child which keeps reference to the next node. It also has a parameter called front and another parameter called back. "Front" refers to the first node of the sequence.But it has a child which refers to the second node. "Back" refers to the last node of the sequence. But its child is a "None".


For me, this week's material is even harder than recursion!
On Monday's lecture, I was really confused by the difference of LLnode and LinkedList. Because both of them have a child and both of them have a value.
I also couldn't tell the difference between some concepts which occur in codes. (For example: "front","next node","current node".)
After class I asked my professor for help. She showed me a picture(as following)








Just as indicated, "Back" is the last node of a sequence and "front" is the first. However, "front" contains a child. So, in fact "front" indicates the whole sequence. But we often use the expression "current_node = front". In this case,
we can use "current_node = current_node.next" to move forward.
By the way the difference between LinkedList and LLnode is that LinkedList is consisted of LLnode.




















Like the code above, we want to get the value of a particular node. So we use expression " current_node = front" to initialize and "current_node = current_node.nxt" to move forward.
INSIGHT: we can't write "current_node.next" because next if a function in python.

Sunday, March 8, 2015

week 8: my impressions of week 7 (btnode)

In week 7, we talked about binary tree and binary search tree.

Binary tree is a tree whose nodes have at most two children.Binary search tree is a binary tree in which the value of left child is less than the value of node and the value of node is less than the value of right child.

Prof Diane drew two pictures which are quite useful for us to understand the concepts in her class and I drew them again using computer and post here.
The left picture represents binary tree and the right represents BST.


 


In lecture we also talked a very important thing which we should keep in mind. That is :"__init__" in class "binary_tree" is different from class "tree".







We set "left==None" as a default. This means if a node don't has a left child then its "left" will still exist and its value will be None.


Although binary tree and binary search tree were not hard to understand, I still met a question in this week's lab!

The head of the question is list_longest_path.










I got confused about how to get a whole path! The only thing came to my mind is that I could write a recursion to create a list holding all nodes. But I didn't know how to get a particular path! Even worse, I didn't finish all the questions in my lab time so I didn't have a chance to ask my TA about it.

Then I posted a question on Pizza! To my surprise, my prof answered it quickly!
According to her ,my should take advantage of some properties of binary tree. That was : "binary trees have only two children." so  we can check them one by one. After some test, I got it.

Here is the solution which is posted on course website for this function.






Sunday, March 1, 2015

week 7 summary of recursion

I want to start this week's SLOG with a gif picture.

  This is my first impression with recursion when we talked about it in week4's lecture ! 
  
  Recursion is a kind of arithmetic which we call itself in its function body. It is the most difficult concept which I have learnt because it's sometimes difficult to trace a recursion, not to mention we write it ourselves.
  
The way I solve this problem and try to understand recursion is practice! After going through all of the lab materials and additional problems, I am much more familiar with recursion.Frankly speaking I go on well with assignment 2!

There are some thing we need to take care when we write a recursion.
1. we need a base case. Without a base case , a recursion function will run forever.
2. for a base case, we need return a value. Only in this case we can call the base case in more complicated situation.
3. the list expression [ fun(x) for x in ] is quite useful in recursion.

However,is there some  patterns for writing recursion?
YES!
I used to follow the following steps.
1.I consider the base case. Regard the object as the simple way and return the expected value.("if expression")
2.Just have a try! Write the "else" expression using the base case! then you have to design a complicated example and trace it. You may fail in the first few times. But I always get it in the end.


Here is a example .










For function sum_list,
1. we consider the base case.(ie. 1) then sum_list(1) = 1
so we write "if expression" as following:






2.we write the "else expression".
First I try like this:






But after tracing a few complicated example, I realize that this obvious wrong because we return a list! so we add "sum"! then we get it!








In summary, after few week's studying, recursion is not that difficult for me and it is a quite efficient way to solve problems of different depth.