Saturday, February 28, 2015

Week #7: Summary on Recursion

Week #7: Summary on Recursion


Now I will summarize on tracing the recursive functions, writing recursive functions and  talking about 'Tree'.

Firstly, let us think about what we should do in tracing the recursive functions. As we discussed in week 5, the most important thing is not what the answer your get by tracing a single case, but what the function wants to do with the attribute. We may easily trace a function case by case by using the result we have get in previous cases, for it is an instinct that using old tools to fix new problems. I think that we use recursion for we want the computer to inherit that instinct. The code we write is an extension of our mind, a baby of our mind, or even a clone of our mind! So what the function wants to do is just what the programmers want to do. That is the only reason for us to trace the function and use the function -- to do what the programmers want to do.


Secondly, what I have learned in the lecture and tutorial is to write my own recursive functions. At the beginning of writing recursive function, I find it really hard to write the code dealing with much harder cases by using the function itself again and again. For sometimes, we may find it difficult to decide whether to use sum() or max() or all() or any() or adding 1 to sum(), to max() or something even more stranger. How do we know what built-in function do we need to use for recursion? It depends on the purpose we write the recursion, that is I emphasize in the previous paragraph-- what the function wants to do with the attribute. As for the example count_list(), by the docstring, we know that we are going to count the number of lists in a possibly-nested list. So what built-in do we need to use? Apparently, it is 1 + sum() for the words: 'the number of'. Another situation, if we want to compare something, we may use max() or min() instead. Or we want to return a boolean, we may use all() or any() representing 'and' or 'or'.




Finally, I want to talk about stack, queue and Tree. Stack and queue are just like a pair of brothers. Stack is to remove the last element in the list, while queue is to remove the first one in the list. But when adding a new element to the list, they both add it to the end of the list. As for Tree data structure, we have introduced both Tree and binary Tree. We can see the concept of a Tree in the picture. I think that the Tree class is not only designed to practice writing recursions, but to build a mind of thinking problems using the Tree data structure. Just like what we do in assignment 2. We analyze the minimax strategy by dividing each possibility of next state to a child node of the root node. And we just need to analyze the leaf node, that is a win state or a tie state. That helps us to determine the current score of each player and help the players to decide which is the best move they should take in order to win the game.

In conclusion, recursive functions and Tree data structure is what we focus on in the previous weeks, and we need to keep them in mind when solving problems related to analyze the situation by analyzing a more original one.

Saturday, February 14, 2015

Week #6: Summary of Object-Oriented Programming concepts

Week #6: Summary of Object-Oriented Programming concepts


So now I want do a summary of O-O programming.In Wikipedia and our lecture, It is defined as a programming paradigm based on the concept of 'object', which are data structures that contain data, in the form of fields, often known as attributes; and code, in the form of procedures, often known as methods. In python it is class-based, meaning that objects are instances of classes, which typically also determined their type. We use the object-oriented programming while dealing with a specific situation in our lives. 

One of the most important concepts is ADT (Abstract Data Types), i.e. creating our own type by implementing classes. ADT helps us to specify the intended meaning stored in the data, and the operations it provides in the data. In python, we use classes to specify that kind of data. Attributes and behaviors are the important part of a class. We implement the class by methods of __init__,  __str__,  __repr__ and so on. 



Just like what we do in assignment 1-- creating a class representing a game state when we play a simple game, a class representing a move made by a player, and a class representing a strategy the computer use to fight with players. By doing so, we can just create a game view class to do the work running the game, by dealing with each game state by prompting the player to enter their move or using the strategy the computer use to make a move. During the work of assignment, we need to find how to take moves and how to do the move to the state, for when the computer use the strategy we set, it need to know what moves it can take and when running the game, we need to know how to change the state. To solve the problem, we need to write the method 'possible next moves' and 'change the current state to next state', which is used to find what moves can we do in the current state and after the move what will the next state be. When doing the possible next moves, we are just to find the square that is less than the current value. When doing the change state, we are just to change the player to the opponent and make move to the current value, that is subtract the move to the value. And also, we need to judge whether either player is win when a new state come out. In the subtract square game, to judge the game over state is quite easy. We just need to make sure whether the square is 0.

Recommend:http://computersciencemaggie.blogspot.ca/2015/03/summary-of-object-oriented-programming.html?showComment=1428298896513 
This is a quite specific and comprehensive and thorough explanation of O-O programming. Good slog to review and comprehend the concepts.

To see slog week 3-8 http://148wuboyangw3.blogspot.ca/search?updated-max=2015-03-08T22:46:00-07:00&max-results=7

Saturday, February 7, 2015

Week #5: Tracing recursion

Week #5: Tracing recursion


We have just learned how to trace recursive functions and write simple recursive function by ourselves in week 4. It is very interesting to trace a recursive function. In the procedure, we need to trace the recursion from the simplest case. And what cases we have trace before can be used as results in the new and harder cases. 

Just like the example in the lab:

I think that one of the difficulty in my lab is to find the function want to do with the attribute. We may easily trace a function case by case by using the result we have get in previous cases. But how can we know what the function want to do? I think we need to focus on what we need to do with the next case in the tracing. For example, we have traced the first 3 cases: nd('ox'), nd([]), and nd([1, 2, 3]). Now for nd([1, [2, 3], 4]), we use the result that  a int is just to return 0 and a list without elements of lists is just to return 1, and we find the max of the nd(elements in the list), and then + 1 at the end.  It is surprising to find that we just to add one if there is a '[]'. So we find the purpose of the function is just to find the number of nest in a nested-list.


Another problem I faced in the lab was how to write a recursion on my own. For example the length function. How to write recursion? I find it simple after analyze what need to be use time by time in our function. The simplest case in length is when L is not a list, then have: if not isinstance(L, list): return 0. Otherwise, we need to compare len(L) and length of the sublists of L, i.e. len(L) and length(x) for x in L. So we need to write the function as:else: return max(len(L), max([length(x) for x in L])). Then the recursion is written! So interesting, isn't it?

Recommend: http://computersciencemaggie.blogspot.ca/2015/03/summary-of-object-oriented-programming.html?showComment=1426384453547
Same idea as me, but much more specific processes are provided in this slog.