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, 

Sunday, February 1, 2015

SLog#2. "Impression of the first few weeks"

I ran into some troubles during the first few weeks but I also learned ways to make improvements. While I was writing the quiz at the end of the second lab, I sort of panicked because I was not able to figure out whether I should return values from a tuple or a dictionary. The problem was that I am so used to building and writing codes as opposed to reading written programs and figure out their functions. It took more time to understand given class than to implement its subclass. A valuable lesson here was that programmers should have a skill to read codes that are already written and understand their purpose in a timely manner.

Though parent class presented in the quiz collected data in tuples, I had thought that the data were being collected in a dictionary. However, I had forgotten how to return values of each key in a dictionary, especially when the key had several values. This type of object was used a lot for the last assignment of CSC108 but I still could not remember it well. Maybe it did not come straight to my head because I was freaking out. But, I realized that reviewing concepts that I learned in CSC108 is very important. Everything that was taught in CSC108 is used in any situation in programming and thus to familiarize myself with those materials would help me establish a strong foundation in programming.

Another point to remember is that, as professor Tom Fairgrieve (CSC108) and Diane Horton said during lectures, it is beneficial to practice coding on paper. When we run erroneous codes on python, it will automatically tell us what went wrong. However, when we write codes on a piece of paper, we can trace any possible errors ourselves. Such way of learning is more intuitive than getting answers automatically. Also, several studies have proven that one way to maximize exam performance is to condition studying environment to mimic an exam environment. Since all of our exams and quizzes will be written on paper, rather than being typed on computers, practice on paper will better prepare us for tests.