Search This Blog

Friday, March 13, 2015

The pain of recursion, and more

Recursion, recursion, and recursion!

Ok, so recursion is important concept. It seems I can't get away from this in this course. All the labs, assignment 2 was about recursion. And yes, the assignment! Minimax! Oh, you minimax! I still don't understand why I couldn't write this properly. I understood what is going on, and knew which part is recursive. But then, why is it so hard to make it work? I still can't figure it out what I am missing.

I see some students who are good at this and say that recursion is easy. Well, I have nothing to say. Recursion is not easy for me(, yet.) I wish I could naturally understand recursion like one of those guys, but I guess I have to take an honest, work-hard path to get to the level I want.

Generating Tree using nodes and children felt abstract. Sometimes, it is clear what I am doing but sometimes, depend on objects, it is quite hard to imagine how Tree is generated. For example, Minimax from assignment 2 was quite hard to bring the concepts into my head. Game state represents the state of current game and it is NoneType. Basically, Minimax uses this current game state to look all possible moves that can be chosen and suggest a strongest move based on this data. Of course, game state can be expressed as a string form, but I needed to use more to use Minimax.

I could tell where it's recursive but I couldn't write right recursive code. The assignment due is passed so there is nothing I can do any more, but I still kinda feel uncomfortable. Unsolved problems leave me such an uncomfortable, unpleasant feeling. Furthermore, despite of my frustration, the class goes on.


    def append(self, value):
        ''' (LinkedList, object) -> NoneType

        Insert a new node with value at back of lnk.

        >>> lnk = LinkedList()
        >>> lnk.append(5)
        >>> lnk.size
        1
        >>> print(lnk.front)
        5 ->|
        >>> lnk.append(6)
        >>> lnk.size
        2
        >>> print(lnk.front)
        5 -> 6 ->|
        '''
        new_node = LLNode(value)
        if self.front is None:
            self.front = self.back = new_node
        else:
            assert self.back, 'Unexpected None node'
            self.back.nxt = new_node
            self.back = new_node
        self.size += 1

So, this is a part of class LinkedList. At first, I was thinking about literally linked list, like multiple lists are somehow handcuffed each other kinda shape. It turned out I had a wrong concept. Just to help myself remember what this is... Linked list is created based on nodes. Suppose there are some nodes created with numbers like the docstring above. Initial LinkedList will be empty when it's called for the first time. Using append, I can append node into the LinkedList. Now, there is one node and the size of link is 1. If I insert another one, the new node will be at the back of the link, and the existing node becomes the front one. The size also changed to 2, the numbers of items in the link. Not only can I append items at the back of the link, I can also insert at the front, or check an item in the link. I found that drawing the pictures help me to figure out how each object becomes different attributes. 

I'm still little behind on all Tree stuff, so adding LinkedList on my study list is little burden now. But what can I do, I will keep working one step at a time. 

 

(Cheer me up, Fry!)

No comments:

Post a Comment