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.

Sunday, February 15, 2015

Slog#4. "Summary of Object Oriented Programming Concepts"

As the blogger at http://lhzuigao.blogspot.ca/ mentioned, concepts of object oriented programming (OOP) are very different from those we learned in CSC108 where our main focus was on detailed mechanics and rules of python coding. One of the most significant differences I have realized thus far in the course is that my coding looks simpler, cleaner, and yet more sophisticated with OOP approach. To this end, I am going to lay out a summary of OOP concepts that were discussed in this course.

1. Abstract Data Types (ADTs)

Objects can contain information and behaviors. In CSC108, we learned how to store information that an object represents in different types of structures, such as dictionary and list, and to implement functions that uses these structures. We now move away from details of implementation and focus on Abstract Data Types which combines information and behavior in one data structure. ADTs specify a set of behaviors (or operations) and their purposes. As shown below with the example of Stack, details of how we should implement operations or store information are not considered here and remain “abstract”.

a) Stack
Stack is an example of ADT that stores items in any structure we want to use (because it is “abstract” data type). Possible candidates for storage include lists or dictionaries. Operations that can be used on a stack ADT are:
i) pop: remove the last item of a stack
ii) push: add a new item onto the top of the stack
iii) is_empty: tells us whether a stack is empty

b) Classes
In python, data of similar types are specified by classes. Once I know what to expect for my ADT, I can start implementing it by defining a new class.

c) Methods
Functions that are defined in a class are called method. Every class, newly defined or existing, inherits special methods from a common ancestor, or an object. These special methods include the followings:

__init__: This method is called when an instance of a class is created. Instance variables are created if the class takes arguments

__str__: This method is called to print an instance of a class.

__repr__: This method returns a string that can construct an object with the same value.

__eq__: This method compares two instances and returns True if they are the same otherwise returns False.

Important note to make here is that a special method defined in a given class may act differently from the same special method defined in another class or a similar module-level function. For instance, we might want to add an extra argument to the method __str__ in our class even though class str at module-level does not take an argument when instantiated.

d) Inheritance and Exceptions
As mentioned above, classes inherit special methods from a common ancestor. Any classes may have subclass which inherits attributes and methods defined in its superclass. These methods will behave the same way unless we override them by replacing with our own methods. When we want a method to work in specific cases rather than in general cases, we raise ‘NotImplementedError’ in superclass and define it in the subclass. Whenever a method is called to an instance of a subclass, Python will check if that method is implemented in the subclass first. If not, Python will use the method defined in the superclass.  

2. Recursion
Sometimes we see a function being called within it. Such functions are called recursive function. For the problems that are solved with recursion, we can derive same results by using iterations such as for- and while-loops. However, with recursion, amount of repetitive codes can be reduced significantly.



3. Trees and Binary Search Trees
A Tree is a set of nodes that may contain values. An edge connects a pair of nodes, one node being a parent and the other being a child, such that there are no edges between two parents or two children. There exists one node at the top of the tree called a root, and every node has exactly one parent except the root of a tree. A path of a tree refers to a sequence of nodes and the edges between them and the length of a path is determined by counting the number of edges on it. A node with at least one child is called internal node while a node with no children is called leaf. Within a tree, subtrees can be formed by any node and its children and edges connecting them. Height of a tree refers to the number of nodes in the longest path of a tree and is determined by adding length of maximum path and 1. Depth of a node is determined by subtracting height of the tree rooted at that node from the height of the whole tree. Finally, maximum number of children that a node can have is defined as arity or branching factor.


An example of a tree we learned in class is a binary tree. Unlike a tree where each node was allowed to have unlimited numbers of children, each node in binary tree can have up to two children (left and right child). 

Sunday, February 8, 2015

SLog#3. "Tracing Recursion"

When I attended the lecture on recursion exercises, at a first glance, I thought the material was pretty easy. I made a huge mistake there and tried to solve all the problems in my head, instead of getting to the conclusion by carefully writing each step down on a piece of paper. As I encountered more complicated problems, like the ones that appeared on the lab #3 exercises, I could no longer come up with a solution without writing down steps. However, when I actually followed every step that were specified in the lab #3 handout, I could more easily and accurately come up with the answer. Even when I ended up getting incorrect answers, it was much easier to locate mistakes I had made. It did take a little while to get used to this process of breaking down into little steps and writing them, but I also learned that skipping steps never really speeds things up. It actually takes less time to reach conclusions when writing all the steps down than when attempting to process all the thoughts in my head. Practice in such a way helped me a lot during the midterm as well because it did not take long to solve the recursion problem, and I was better able to allocate the limited time we were given to the other problems.

While I was in the office hours of CSC165, I learned something very similar from the TA who was helping me with the Assignment #1. When I asked him questions regarding translation of logical notations into English sentences, and vice versa, he always made me break questions down into smaller segments, write translation for each of segment, and then combine everything at the end to derive answers. As such, the important lesson of this week was that in computer science, or perhaps in other disciplines, any complicated problems should be approached by breaking them down into simpler steps. Also, keeping records of thought processes always guide to more precise answers,