Sunday, April 5, 2015

Last impressions

Over the course of this term, I learned many new things. First of all, I learned more about classes and got to know two new types of abstract data structure, namely Stack and Queue. I learned to write different methods for the classes, such as __eq__, __repr__, __str__ etc.. I found out that classes can build on a preexisting class, and adopt all of its features using inheritance. This increases efficiency because it reduces the need to write duplicate code, if one or more classes have common features with another class.
I also learned about tracing and writing functions. Recursion was very hard for me in the beginning because I tend to lose track of what the code does when it calls itself again without finishing the initial call. Luckily, I had help from my friends and Prof. Danny. They traced a couple functions with me and cleared a lot of my confusions. Writing recursive functions was also fairly challenging. I learned that to write them, I need to consider what is the simplest case that does not need recursion (which is the base case), and then ensemble these simple pieces of information together that returns a desired result.
Trees (and binary trees) are applications of recursion. Trees have a recursive nature in which one children can branch off and have many children and so on. I had a lot of practice in using recursion during tutorials and assignments, in which many tasks involve finding how many internal nodes there are, or returning particular values at the leave nodes. Linked lists are kind of like trees in which they also have nodes, but are ordered in a linear fashion. In beginning, I was confused of why there are two classes for linked lists, and what instance variables to refer to in writing code for them. But later I learned that, linked lists involve a collection of nodes. Hence, one class is specifically one node, and another class is a wrapper that stores the collection. Trees and linked lists are two new data structures that I learned in this term, which I think will be very beneficial in solving problems in the future.
Towards the end of the term, csc148 had a lot of overlaps with csc165. I learned more about algorithm's running time, worst case running time, and asymptotic representations of these running time. I learned that running time varies depending on the size of input (n) and I saw examples of code that is big-O of n, n^2, n^3 and lg(n). These notations allow us to better analyze and compare the efficiency of different codes, which is very helpful in terms of choosing the better option. If there are more options available for achieving a same task, but one has a running time in O(n^2) while another has O(lg n), then the latter one is much of efficient for users, because when input size n gets large, n^2 is huge and takes a long time, but lg n is much faster. 
This term taught me a lot of useful techniques, like tracing functions, writing recursion and many new data structures, like Stack, Tree etc. They combine to prepare me to be a more efficient problem solver. Although I had challenges along the way, csc148 was certainly useful and heuristic, as it gave me practices on logical reasoning and problem solving.

impressions of week 9

In week 9, I learned more techniques on choosing test cases and tracing function calls. In lectures, Prof. Danny showed us one possible solution to minimax. When I was working on assignment 2, writing functions to make minimax work was the most challenging task. I thought that every game state is a node, but didn't know how to return a move that leads a winning state. In lecture, I learned that tuple is a choice of good data structure that can be used to link a move with a state. An initial state have a list of possible moves, and each of the moves can lead to another game state, and so on. Because of the nature of the data, recursion is the suitable strategy to return what initial move can lead to a final winning state (if there is one). I learned that recursion can be applied to each of the moves, until the very end, where a player wins. That final game state would generate a score of +1 for the player who won. Once the leave nodes are calculated with a score,  recursion can trace back the path that led there, thereby suggesting an initial move.
On top of learning the algorithm on solving minimax, I also learned in class of how to trace function calls. For example, the following program, when run with python, would generate a value of 12. However, the process of reaching there involves many steps. I learned that python prioritizes an input value over another variable value, even though they may have the same variable name. When h(1) is called, the function h(N) is called, with N=1, but not 5, even though that the variable N is given the value 5 in the beginning. Furthermore, because h(N) involves a nested call for both functions g and f, each time the input N is changed according to the value returned from the nested call.
Tracing calls like this seems really easy and basic, but it is a very good practice which helps programmers to follow through the code and see what the code does at each step This can be very beneficial if in the future that we ever have to write complicated programs that involve many variables.

Sunday, March 15, 2015

impressions of week 8

In week 8, I learned how to mutate binary trees. To insert nodes into a binary tree, the algorithm uses recursion. It needs to check whether if the tree is there, because if it is None, then I can just create a BTNode of that particular value to be inserted. This is the base case for recursion. If the tree is there, then I need to compare whether the value to be inserted is less than or greater than the root node. If it is greater than the root node, I can just keep calling the insert function on node.right to insert the value into its appropriate position. Likewise, if the value is less than the root node, call insert on node.left to insert the value. And if the value is the same as the root node, then there is nothing to do, because the tree already has that value. 
I also learned the algorithm of deleting a node. It is very confusing for me to understand, because nowhere do I see in the code that 'deletes' a node. 
As seen on the left, the code for delete does not have any operators like 'del'. I was confused and so I asked my friends after class and understood that the delete function 'deletes' a node by making the original memory address of the value that a node contains lost. This way, that node is deleted. 
However, I still do not really understand what does the else statement do for the delete function, so I plan to seek help in the week to come.

Sunday, March 8, 2015

impressions on week 7

This week's focus was mainly on linked lists. I learned about binary search trees and got introduced to linked lists. Unlike trees, binary trees only have 2 children. Therefore the variable self.children cannot be a list anymore, but instead, it is split to self.left and self.right. This is a lot more convenient for searching algorithms to be implemented. However, it also brought difficulty in writing code because self.children can be an empty list, which can allow programmers to still use list methods on it. But if self.left or self.right is None, then it is harder to write code without errors. 
As mentioned before, because a binary tree only has two children, it is very efficient to search for an item in the tree, if the tree is sorted. This is much like linear search, in which both cases need the data to be sorted. I learned that a binary search tree is a sorted tree, in which all the values less than the base node would be placed to its left, and all the values greater than the base node would be placed to its right. This means that to search for an item, we do not need to compare each node to the value we are searching for (in which case it would require a longer processing time), but rather, one comparison can throw away half of the tree values off (for that they are disregarded from further searching). It is a much faster and efficient way to search for values.
In the beginning, I thought that linked list is the same as nested list. However, it turned out that they are not quite the same. Linked list is also a class object, which has front, back and size. However, I'm still quite confused of the connection between class LLNode and class LinkedList. It was specially complex when involved with recursion. For example, the __getitem__ method was quite challenging to understand, because I did not know what does recursion help to increase index number. But after I asked my friends, I found out that self.size (which is positive) is added to index to keep on neutralize it to pull it towards positive. I was also confused about the else portion, because I don't understand why cur_node (which refers to self.front) has value and nxt, as these variables belong to LLNode. I hope to see more examples of linked lists to continue improve.

Sunday, March 1, 2015

Week 7 Slog

Dear marker:

I wish to have my summary on tracing recursion to be marked. The following is a link to that summary:
http://csc148experience.blogspot.ca/2015/02/impressions-of-tracing-recursion.html

Sunday, February 15, 2015

Summary on object oriented programming

After learning about object oriented programming for the past a few weeks, I acquired many skills in designing my own class objects and creating methods for them. Writing my own classes can allow me to create objects that Python does not provide and evaluate them in the way that I want it to work. Although in the beginning it was confusing for me because I do not really know what ‘self’ refers to. I tracked a lot of the example code in the lectures slide into Python visualizer and learned that ‘self’ refers to the class object that I’m creating, whenever I call ‘self.xxx’, Python knows to access the object with the appropriate instance variable, or the appropriate method of that class.

I think object oriented programming allows me to solve problems in a much more efficient manner. Inheritance, for example, can save me a lot of time in writing programs. With inheritance, I can now create objects that use features from another class that I have already written. This is very useful, because for example, I can create a superclass for my general courses, with features like returning course weight, grade achieved etc. With that, I can then create subclasses for my individual courses, without rewriting the same code, if I want to access the weight and grade for the specific course. Thus, object oriented programming extends the types of objects that Python currently provides, which make it handy and applicable to many different situations that a programmer may need.

Also, with enabling classes and instance variables, any client can create the object of the classes and use the methods written for them, without needing to know how the code achieves what it does. Just like I do not know what the code is for Python’s existing methods on its objects, like lists or dictionaries. But all I care is that I can use the append method to add items to lists, and the del method to delete keys in dictionaries. With classes, I can create my own objects for clients to use, with proper docstring provided to show what the methods do and others can write methods on objects that I may need to use. For example, a stack or a queue has very simple but useful features. Although I’m not the one who designed them, I can use their features and solve problems of my own need.


Learning object oriented programming had combined successes and failures. During the process, there were lots of confusion, especially when designing my own classes. I’m sometimes unable to come up with the appropriate instance variables a particular class should have, and the general methods that every subclass might share. However, it is undeniable that object oriented programming is efficient and useful. I hope to continue improve in the future.

Sunday, February 8, 2015

tracing recursion

Looking back at last week, I had mixed successes and failures in understanding the concept of tracing recursive functions. Recursion is something very different from all the functions I used to see. Its function body calls itself, enfolding many layers. Hence, tracing it is like peeling an onion, I have to peel it layer by layer, and in the end, I just cry.

It is very painful in the beginning trying to follow what the function does for each item in the loop. I had difficulties in figuring out what the function produces, if the function is called into itself again. Luckily, the docstrings and descriptions helped me in understanding what the function is aimed at doing. Many recursive functions we have learned in class involve dealing with nested lists, such as getting the sum of all the integers in the nested lists, or the max value for the values in the nested list. As a practice tracing these functions, I gradually observed that there are always a base case and a loop case. Each time iterating the loop, an item is called into the function again, until the base case is reached and returns a value.
However, towards later this week, I noticed that the docstrings start to disappear. Many recursive functions are presented without the coder describing what it does. This is somewhat challenging. I remember in last week’s recursion tracing quiz, the question was a function that didn’t have description, and I did not figure out an answer. Recursive tracing is still quite difficult for me, consider this example below, I remember I had a lot of trouble trying to deduce what it produces.



I have only figured out what S(1) produces, but starting S(2), and later S(3) and more, I’m confused. I remember I asked my friends about this question, and they appear to be similar to my state. Although I knew that I was not alone, I still wanted to understand it. Eventually one of my friends got the answer, S(2) is ([2 * S(2-1)[0] + 2**1 - 1), which is (3, 1). After he explained it, it starts to make more sense to me. I learned that for recursion, if an example is already traced, the result can be directly substituted into another example, if it contains that portion. I think in the future, I would still need to work on recursive tracing more to even get a better grasp of it.