Saturday, March 28, 2015

Week 7 impression

    The introduction of binary tree node (BTNode) seemed a little irrelevant to the topic. What's more bizarre was the pre-, post-, and inorder.
    BTNode is a tree similar to a normal tree. The difference is that in BTNode, The root has three attributes defined in the __init__ method: data, left, and right. Data is similar to the "value" in a normal tree, left is a subtree that has all the value that is smaller than the "data", and right is a subtree that contains all values larger than the "data". In a sense, BTNode is a sorted tree.
    The pre-, post-, and inorder seems complicated.
        Preorder:
            (root)->(left)->(right)
        Inorder
            (left)->(root)->(right)
        Postorder
            (left)->(right)->(root)
    The above is the way nodes of a tree is orderly visited.
    For example, if we have a tree like this:

                    Root
                   /       \
                A         B
              /    \       /    \    
             C   D     E    F
    Preorder would visit the tree in an order of: root, A, C, D, B, E, F
    Inorder: C, A, D, Root, E, B, F
    Postorder: C, D, A, E, F, B, Root

    However, in the assignment, we needed a tree to create all the possible next moves. What is showed in class seemed to be irrelevant. I can understand what BTNode and those orders are, but no idea where it may be useful.

Abstract Data Type

    What is abstract data types?Abstract data types (ADTs) are ways to store and retrieve data without detailed information about how they are done. That is, for example, what people need is a software that does something, but they don't need to or want to know how everything is put together to work.
    One type of ADTs is stack. A stack is a list of data, which in real life can be a stack of books, but the only way a stack can be modified is through the top one (eg. you can only add or remove from the top).
    For a class stack, we can often see methods like "push", which adds item on top of a stack; "pop", which removes the top item from a stack; "is_empty", which checks whether the stack is empty.
    Another type of ADTs is queue. While other properties are similar to a stack, queue, instead of removing starts from the top, starts removing from the bottom.
    The only difference between codes of stack and queue is the following:
    in (stack) method pop(self):
        return self._data.pop()
    in (queue) method dequeue(self):
        return self._data.pop(0)
    The "_" before "data" is used as a way of hiding information (which is already set up in the __init__ method) so that user don't see it. The clients need only to know what needs to be known, and hiding the data can protect local data being modified either by accident or with intention.

Object-Oriented Programming concepts

    Lets define some concepts to begin with.
    Class is a place where objects are defined (to have attributes that characterize what it is), which works in the way similar to how def works. Class also includes grouping of data and functions, which is called methods. One can imagine class being a blueprint for creating an object. 
    In assignment 1, we were asked to define a game called Subtract Square. In order to create this game, we need to use class to define a it, such as SubtractSquareGameState, to keep track of the state of the game, a strategy such as Strategy_Random, to select the next steps for computer, and SubtractSquareMove, to actually making a move. In the above classes, regardless of how complex our codes are, we defined the fundamental objects such as a move, a strategy, and a game state for the game.
    Attributes of the objects we wanted to create will be defined in an __init__ method, that when the class is called, such attributes will be implemented into the object. Those attributes are built upon "self", which, intuitively refers to the object itself. Methods are then used to define the object using the existing data.
    In the assignment, we also encounter inheritance. For example, from a general class strategy, a specific strategy called RandomStrategy can inherit all the traits from the general class, and it can also overwrite them. The advantage is when there is a number of strategies (general class) we want to define, and we don't want to waste time writing repeated codes, inheritance can be very helpful.
    Next, after we've created the definition of the game, we create objects following those definitions in another file, game_view. We use this file mainly to create all the objects we need in order to properly play a subtract square game, and by importing from previously defined classes, we are able to play the game.
    In the end, this simple game seems to be rather a complex object to create. I would not want to see how much code an actually enjoyable game requires.

Tuesday, February 24, 2015

Recusion

    Recursion has a somewhat tricky logic: A sublist inside a list that has the same format as its superlist. Yet recursions are always of the same format, for example, a sum recursion is as followed:

def sum_list(x):
    if isinstance(L, list):
        return sum([sum_list(x) for x in L])
    
    else:
        return L

    So it seems like the first thing to do is to check whether input L is an instance of list, that is, to check in the children node (which don't have subordinate level nodes) and see if it follows the basic conditions that we want, and in this case, L is checked to be a list or non-list: if its a list, there is a need to sum up the numbers within it; if not, the value L should be returned.
    Then go inside the list, using recursion and sum up the elements of children, and using the sum_list method itself to repeat the steps with its superclass and so forth. In the process, values from each subordinate level children are gathered into a list, from the very bottom level. The sum function sum up those values, but because sum function calls the list of values using sum_list(x), which is the function itself, the recursion occurs. For every element in list L, sum_list trace into the inner element of it through "for x in L", in a for loop way, and get a list of values in the inner list. The outer sum([...]) then sum up those values and return it to a superordinate level. The process repeats and we get the results.
    Once the process is understood, a recursive problem is not too hard.

Monday, February 2, 2015

Week4

Terrible, terrible, terrible.
Spent a ton of time on assignment, alone. Found it so difficult that I could not start it. Then started writing every process down, or least trying to. After that, created some methods about specific game. Still confused about how many classes was supposed to be created, but the it got clear after searching on Piazza. However, methods written were exactly helpful in the attempt to do GameView. Asked a friend, spent two entire day, and problem solved.
Conclusion: for someone who sucks at writing codes, use all the resource you can get -- especially a friend who does well in CSC.

Thursday, January 22, 2015

Why geeks should write

Why should people learn computer languages. When we have to keep in step with technology, computer science seems essential for us to deciphering the increasingly more complicated yet simplified world. Why should geeks write? When computer languages open up a completely new world, so convoluted that we have to convert our thoughts into simpler human language in order to learn from them. That's it for today.... Gotta sleep.