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.