2015年3月1日星期日

WEEK8: Impression of week 7, debugging on my assignment 2.

While I was doing my assignment 2, a bug of alias problem really gave my group a hard time.
We have a class of TippyState having TippyState.current_grid containing a list of list representing the current grid of the game state.
In the method TippyState.apply_move(move), we first have 

new_grid = self.current_grid
(then we modified the new_grid to the grid of the game state after the move is applied)
return TippyState(new_grid)

The problem happened when we modified the new_grid, because our modification changed the self.current_grid as well.

Then we tried:
new_grid = self.current_grid.copy()
(then we modified the new_grid to the grid of the game state after the move is applied)
return TippyState(new_grid)

We thought it would work because we used list.copy() instead of just assigned the value of self.current_grid to new_grid, so we thought the modification of new_grid should not affect the self.current_grid.
However, this is still WRONG!!! Both self.current_grid and new_grid are list of list, which means, although the outer lists have different ids, the inner list still share the same ids. When we are changing the content of the inner list, both self.current_grid and new_grid are changed.

conclusion of the problem:
L = [1, 2, 3, 4]
L1 = L
L1[0] = 0
L1 
[0, 2, 3, 4]
L
[0, 2, 3, 4]

L = [1, 2, 3, 4]                                   
L1 = L.copy() 【or L1 = L[:]】                     
L1[0] = 0
L1
[0, 2, 3, 4]
L
[1, 2, 3, 4]

L = [[1, 2], [3, 4]]               L: id1      id1(list):[id2 ,id3]      id2(list):[id4, id5]   id3(list):[id6, id7]
L1 = L.copy()                     L1:id8     id8(list):[id2, id3]
L1[0][0] = 0                       id2(list): [id9, id5] 【so both L and L1 are changed】
L1
[[0, 2], [3, 4]]
L
[[0, 2], [3, 4]] 


At the end, we used a for loop to append every list.copy() of self.current_grid on to new_grid, only in this way, we can make the inner list of current_grid and new_grid have different ids.

So yeah, we should be very careful dealing with lists, especially with copy of lists! of lists! of lists!


WEEK7 Please mark my OOP post.

I don't know if you will go into my OOP post and mark it or somehting.. but anyway I have copied my entire OOP post here.

In an Object Oriented Programming language such as python, everything is referred as an object.
A list is an object;  a class is an object; a function is an object; even None is an object known with the type 'NoneType'.
In python, every value is an object and has a memory address. For example, when we type 'x = 3' into python, it does not mean that x has the value of 3, but instead, it means that x is pointing to a certain memory address, and such address contains the value of 3. So, in another word, an object is a piece of memory.
Different objects could have the same value. For example, if we type 'x = 3' and 'y = 3' into python, python will create two objects separately in memory id1 and id2, and both memory address will contain the same value known as 3.
Objects could contain both information and behavior. Given an example of a list object, it contains the information of a list the memory address that are pointing towards the values of the values in the list, and it also contains behaviors such as sorting the list or measuring the length of the list. We use a dot '.' in python to call those methods of an object.

About Object Oriented Programming.

In an Object Oriented Programming language such as python, everything is referred as an object.
A list is an object;  a class is an object; a function is an object; even None is an object known with the type 'NoneType'.
In python, every value is an object and has a memory address. For example, when we type 'x = 3' into python, it does not mean that x has the value of 3, but instead, it means that x is pointing to a certain memory address, and such address contains the value of 3. So, in another word, an object is a piece of memory.
Different objects could have the same value. For example, if we type 'x = 3' and 'y = 3' into python, python will create two objects separately in memory id1 and id2, and both memory address will contain the same value known as 3.
Objects could contain both information and behavior. Given an example of a list object, it contains the information of a list the memory address that are pointing towards the values of the values in the list, and it also contains behaviors such as sorting the list or measuring the length of the list. We use a dot '.' in python to call those methods of an object.


What did we do in week 4.

In week 4, we learned about recursion.
In a recursion, first of all we have a base case. Base cases are always very simple, and most of time they can be testified  within a few lines of code.
Besides the base case, a recursion also have constructor cases. When a recursive program with a given input failed to be testified as one of the base cases of such program, the program would call on itself with some new input which are decided by the original input. Then the program go through the same process of testifying whether the new input belong to the base cases. Hopefully, the program will finally reach the base case and then it will return the output value all the way back to the program with the starting input.

When writing recursive functions, we should be aware that our program will reach the base case gradually, otherwise the function would go into an infinite loop.

2015年1月29日星期四

Things from these weeks, and also about assignment 1 and the recursions.

CSC148 started scarily, especially with its scary assignment 1.
To be honest, class was the part which I didn't really understand during CSC108H, so designing classes was really a challenge.
But after a few weeks of study, I think I have a way better understanding on classes than before. Therefore, I do hope do get a nice mark on my first assignment. XD



Recursion
Recursion is a function(or maybe later a method, but anyway a way of doing things, i will always refer it as 'function' in the rest of this post) which calls itself.
When we use recursion:
1. when we can solve a big problem by solving its components using with the same concept.
2. especially when we don't know how many times we need to call the function. (We don't know how deep we need to go.)

the typical appearance of a recursion is:
def f(L): #f is a program and L is the input
    if  isinstance(L,x): #x refers to a type, it could be anything.
        #thing here could vary, but anyway it calls f on the components of L.
    else:
        return XXX #return the basic value of this function.


Things from assignment 1:
- doctest doesn't test on print or input, so need to be careful for some potential mistake there.
- when printing, the white space due to indention will be printed as well, what I did to solve this problem is to delete the indention.

why geeks should write?

Well, geeks should write, not to eulogize the beauty of the life or the glory of the world, but to, for most of the time, build a better understanding of computer science.
Writing is good to keep track of what happened. Writing down the coding experience is helpful for future coding process. After recording down a bug we found in our codes, we have the reference source to look at when next time encountering the similar type of bugs.
Writing is also a good way to recollect the material that we have learned already, in another word, it’s a good way of reviewing. We don’t learn stuff by just going through it once; however, going through the same thing over and over again seems too boring. By writing, we can achieve a good understanding of the materials that we learned.
There is a Chinese old saying known as people gain new insight through reviewing old materials. By writing, we are organizing the old materials in a way such that it’s convenient to come back in the future. After writing down the expiration from the current materials, we should often go back to what we wrote previously so we may gain something new from it.

Anyway, even we were just geeks, we still should write. Geeks write to keep track of what happened, and to prepare for what will happen.