Sunday, March 29, 2015

Slog #9. "revisiting previous slogs"

    My views have changed considerably since the beginning of this course. Generally, I tend to think more about the big picture rather than small details. In my Slog #4, I wrote that objective oriented programming (OOP) leads to simpler, cleaner, and yet more sophisticated codes. At that time, I had thought that OOP was all about applying new concepts that are covered in this course. New concepts are important and application of those is crucial part of this course. However, I learned that it is more important to think about the big picture before implementing any codes. In other words, thinking about what a function or method is supposed to do, rather than worrying about which recursion should appear in which line of the codes, is always the best way to start implementing.


    I used to write codes without much thinking. I merely tried different things by changing what I thought was wrong just to get it to work on doctest examples. Then, I moved on to more complex examples. This was very time consuming as sometimes I had to come up with whole new codes to get it work on more complex examples. However, I now think about big pictures before doing anything. I first pick out a set of examples with different starting points and results. Then, for different examples, I plan out what the next step will be until I get to the result without worrying about the concepts or techniques that I will be using. Once I get a good idea of what is going to happen, it is so much easier to implement. 

Sunday, March 22, 2015

SLog#8. "Binary Search Tree Mutation"


    We revisited binary search tree (BST) and implemented new functions that allowed us to mutate its nodes by adding or removing wanted nodes to the given BST. Though I did not have much trouble comprehending/implementing insert function, delete function was very difficult.


    For insert function, all we really have to do was to add a wanted node on a left side of BST (or left child) if its data (or node.data) is smaller than node.data, otherwise on a right tree. On the other hand, we need to consider more cases to implement delete function. Firstly, we need to check whether value to be deleted exists on the tree. If the value does exist, we need to check if it’s larger or smaller than a node’s value and send it down to appropriate side of tree. In cases where node to be removed has only one of either left or right child, the existing child will take the place of removed node.  However, if the node to be removed has both children that are not None, node with maximum value from the left subtree will take its place, and this maximum value node will be removed from its original place. 

Sunday, March 15, 2015

Slog#7. Linked Lists


Linked list was another complex topic to comprehend. I was really confused when we implemented methods such as append and prepend. Methods prepend was easier to comprehend and I had almost no difficulty implementing these methods on my own. However, append method was a lot more challenging. The main problem I had for prepend was that I did not quite understand the point of putting the following in prepend method:

if self.back is None:
self.back = self.front

When I tried printing an instance of linked list class, it seemed to work fine with these two lines commented out. However, I learned that not putting those lines will be problematic because we need to keep track of self.back at all times which is not instantiated when the class is created. Unless we specify which value self.back is going to take, it will remain as None.

It was much more difficult to comprehend append method. The main problem here was that I could not figure out how self.back.nxt = new_node changes self.front as well. After following every step in python visualizer, I learned that when self.front = self.back= new_node is called at first, both self.front and self.back refers to the memory address of an object which contain LLNode(5). This node has no ‘next’ node associated with at this point. Subsequent append method calls will lead to self.back.nxt = new_node and now the memory address with LLNode(5) has a next node associated with it such that now it looks something like LLNode(5, LLNode(6)), for instance. Consequently, self.front changes accordingly as it also refers to the memory address of this node which has a value of a 5 and is associated with next node LLNode(6) which itself has a value of 6 but has not yet been associated with a ‘next’ node. When this node is connected next node, self.front will change again accordingly. 

Sunday, March 8, 2015

Slog#6. "Binary Tree, Binary Search Tree"

This week, we focused on class binary tree (BT) and binary search tree (BST). Unlike class Tree, which may have unlimited number of children, BT and BST can only have maximum of two children per parent node. Due to limited number of children each node can have, I thought BT and BST would be somewhat simpler to deal with. However, as the blogger at http://akcolter.blogspot.ca/ mentioned, I found this week’s materials significantly more complicated than what we have learned thus far in this course.
Though I thought I had a clear understanding of recursive methods, writing BT and BST methods with recursion was much more challenging. The most challenging part was to understand recursion that appeared in __str__ method of BT and BST. At first, I did not even understand why it was recursive methods. Also, I did not understand how __str__ method was able to put a new line character after right_tree when right_tree statement did not contain new line character. After drawing lots of diagrams of different cases, I learned that right_tree and left_tree variables also involve recursion by calling __str__ method on them, if they existed. Therefore, return statement of __str__ method is being called on each side of tree and that is where new line character is coming from. For instance, when __str__ is called on a right_tree that has no children, its own left and right tree does not exists and it will only return '{}{}\n'.format(indent, str(self.data)), where self.data refers to the “root” of the right_tree, rather than the root of the whole tree.
To improve my understanding better, I also worked on the exercise on writing recursive evaluate method. Here, I used recursion to find nodes whose data is float and created a string of "b.left" + "b.data" + "b.right" and called "eval" on this string which returns the result of this expression. This will continue until every node in BST is evaluated and returns one single float. 

Sunday, March 1, 2015

Slog#5. "Choosing a topic to be graded"

I have written the summary of all three concepts. I would like to choose the summary of ADT to be graded for this slog.