In this semester, we learned pretty much about recursion. It used to be a completely new concept to me, and it was even hard to understand at the beginning, but after doing much practice, I now find it concise and, even beautiful!
I revisited my impression about recursion which I wrote a few weeks ago. It turns out that I actually knew the definition well at that time. As I wrote in the blog, recursion is a method of solving problems that involves breaking a problem down into smaller and smaller subproblems until you get to a small enough problem that it can be solved trivially. Usually recursion involves a function calling itself.
However, now I'm starting to get used to it more and think about whether it'll be better to use recursion in the code. Instead of only memorizing the definition, I find it more practical in some coding work. In fact, recursion can just solve the problem in much fewer codes rather than writing several lines of 'for loop' and 'while loop', which is absolutely its advantage. While it may not seem like much on the surface, recursion allows us to write elegant solutions to problems that may otherwise be very difficult to program. And recursion is proved to be super effective in the class such as Tree and BTNode which we did this semester. It's also a crucial method in A3 that we're working on now.
Monday, 30 March 2015
Sunday, 22 March 2015
looking back at a2
In this week's lecture, we looked back at A2.
First, we talked about how to get the apply_move right. In order not to change the GameState according to the type contract, a new method called deep copy was introduced. We use deep copy to make a copy at every level so that we won't inadvertently change the GameState.
Second, we did something about testing minimax. Basically the call looks like:
First, we talked about how to get the apply_move right. In order not to change the GameState according to the type contract, a new method called deep copy was introduced. We use deep copy to make a copy at every level so that we won't inadvertently change the GameState.
Second, we did something about testing minimax. Basically the call looks like:
sm = StrategyMinimax() sm.suggest_move(SubtractSquareState(?, ?))
The important idea is that if one test case passes, it doesn't necessarily mean that the minimax worked well because there is a possibility that it can be passed by a random strategy too. So for example, we tried some test cases in the lecture. It turns out that some of the game state will lead a win for one person no matter what move he chooses. In this situation, minimax won't be tested effectively. However, some game state with some moves leading to a win and some moves leading to a loss is better because the player will certainly win if he uses minimax. When he chooses random move, he's not guaranteed to win or lose.
Midterm
Time to test ourselves!
We had the midterm this week. There're 3 questions on it covering Tree, Linked list, etc. The first question asks us to draw two binary trees with 20 nodes -- one tallest and one shortest. That should be easy if we know well what a binary tree is. And we were also tested on what preorder search is. The second question is mostly about recursion, which used to be a new and pretty confusing thing to me, but now it is not. The last one is about linked list.
We had the midterm this week. There're 3 questions on it covering Tree, Linked list, etc. The first question asks us to draw two binary trees with 20 nodes -- one tallest and one shortest. That should be easy if we know well what a binary tree is. And we were also tested on what preorder search is. The second question is mostly about recursion, which used to be a new and pretty confusing thing to me, but now it is not. The last one is about linked list.
Sunday, 8 March 2015
Impressions of Linked List
As what we learned in this week, linked list a linear sequence of nodes, each with a value a reference to the next node. What's more, we keep reference to the front and back of the sequence. We defined two classes in the program, which is LLNode and LinkedList. LLNode represents a single node and LinkedList represents the linked list as a whole, which can be called a wrapper class. Regarding wrapper class, it means that we can bundle information about the overall linked list. Moreover, an empty linked list is an instance of the class, with attribute values set to show it's empty.
There are three important thins about linked list. One is traversing a linked list. Following is a common pattern:
There are three important thins about linked list. One is traversing a linked list. Following is a common pattern:
current = self.front
while <some condition holds>:
<Do something with the current node.> # Advance to the next node. current = current.nxt
Another thing is mutating a linked structure. Since code that mutates an existing structure is easy to mess up, we are strongly recommended to draw pictures, which helps us understand it better.
The last thing we need to pay attention to is the problem of going to far. For some methods, once we've found what we are looking for, we have gone too far to make the changes we want. An useful solution to this is to walk two references along the list, with one trailing behind a step.
Sunday, 1 March 2015
abstract data type
In my perspective, abstract data type is a model for a certain class of data types that have similar semantics (the meaning of real-world entity being represented as is showed in the course note). It puts more emphasis on the intended meaning of
the data it stores, and the operations it provides on that data instead of talking about the how to store and
manipulate the data in a particular programming language.
As what we learned in lectures in these weeks, it's obvious that it has its own advantages. One of it is that We can think about algorithms, or recipes, for solving problems more freely if we don't have to include
all the details of how our objects are implemented. Moreover, details of how objects, and their components, are stored and accessed vary between programming
languages, whereas a really good algorithm can be translated into any programming language. And stack is a good example which we have learned before.
Subscribe to:
Posts (Atom)