Saturday, March 28, 2015
impressions of week 9
We wrote the second term test last week. It was surprisingly doable for me especially after we had assignment two which was really hard. The test closely connected with the labs which I found really fair and practical to the people who did the works and want to learn something in this course. I must say that labs are playing an extremely important role in this course because theories and codes written by professors are taught in lectures which cannot really change the way we tackle a problem. Only in labs can we focus on solving problems and coding according to those rather abstract theories. And also tests are related to labs rather than assignments which are too hard to solve in 1 hour. It is a shame that labs are canceled due to the strike because it means we would learn less in this course.
impressions of week 8
Linked list was our focus last week. I found it more easily to understand than the previous topics since this class is more concrete and relates the materials from CSC108 which I was pretty familiar with.
There are two different ways of thinking of linked lists structures. One is to think of it as an item and a sublist and the other is to see it as objects with a value and a reference to other similar objects. We took the second perspective in the class. A new class LLNode was introduced as the object in the second point of view. A LLNode has two attributes value and nxt where value refers to the data it represents and nxt refers to the next LLNode. A LinkedList(formed by LLNodes) has three variables, front, which represents the first node, back, which refers to the last node, and size which stands for the number of the nodes in the LinkedList.
Mutation and iteration are related to LinkedList. For example, in the append method, when a new LLNode is appended to a LinkedList, we will have to change the back node, the former back node, the size of the list and probably the front of the node if the LinkedList is None. As for iterations, for example in the contain method, we have to walk through the list to see if the value is contained in it.
There is a general format for iterations in a LinkedList:
cur_node = self.front
while:
# do something here...
cur_node = cur_node.nxt
There are two different ways of thinking of linked lists structures. One is to think of it as an item and a sublist and the other is to see it as objects with a value and a reference to other similar objects. We took the second perspective in the class. A new class LLNode was introduced as the object in the second point of view. A LLNode has two attributes value and nxt where value refers to the data it represents and nxt refers to the next LLNode. A LinkedList(formed by LLNodes) has three variables, front, which represents the first node, back, which refers to the last node, and size which stands for the number of the nodes in the LinkedList.
Mutation and iteration are related to LinkedList. For example, in the append method, when a new LLNode is appended to a LinkedList, we will have to change the back node, the former back node, the size of the list and probably the front of the node if the LinkedList is None. As for iterations, for example in the contain method, we have to walk through the list to see if the value is contained in it.
There is a general format for iterations in a LinkedList:
cur_node = self.front
while
impressions of week 7
We focused on binary search trees and binary expression trees last week.
A binary search tree looks exactly like a binary tree except that it is in a specific order where the data in the left subtree are less than node.data and the data in right subtree are greater than node.data. A main method associated with the binary search tree is the contain method. This method takes a binary search tree and a number as inputs and return a bool that indicates whether the value is contained in the tree. The special order of the BST makes this method much more efficient than before when we can only use a binary tree because it avoids checking unnecessary nodes. With BST we can check whether an element is contained in about lgn nodes where we have to check n nodes with an ordinary tree.
A binary expression tree is a binary tree with node.data equals to an arithmetic expression. When evaluating a binary expression tree, we use recursions as we did in the last few weeks. If the expression is a leaf it returns its value otherwise it evaluates the left tree and the right tree and then combines them with the binary operator. We use the python built-in function eval here.
Sunday, March 1, 2015
Week 7 (Tree ADT)
Please grade my last post on OOP
http://silvercsc148.blogspot.ca/2015/03/object-oriented-programming-summary.html
We are dealing with recursions in the methods contained in the class of tree ADT these few weeks. To begin with, we were introduced to a bunch of Terminologies. For example, internal node is A node with one or more children and arity is the maximum number of children for any node in a tree. Then we implemented the tree ADT which led to the implementation of recursion. The general form of recursion is as follows:
if < condition to detect a base case > :
< do something without recursion >
else: < general case >
< do something that involves recursive calls >
After we got familiar to this abstract data type, for the week just passed, we practiced writing functions that involved recursive calls. The procedure in doing this was essential because they might seem scary at first, but after breaking them down into small steps as indicated above they became doable to me.
http://silvercsc148.blogspot.ca/2015/03/object-oriented-programming-summary.html
We are dealing with recursions in the methods contained in the class of tree ADT these few weeks. To begin with, we were introduced to a bunch of Terminologies. For example, internal node is A node with one or more children and arity is the maximum number of children for any node in a tree. Then we implemented the tree ADT which led to the implementation of recursion. The general form of recursion is as follows:
if < condition to detect a base case > :
< do something without recursion >
else: < general case >
< do something that involves recursive calls >
After we got familiar to this abstract data type, for the week just passed, we practiced writing functions that involved recursive calls. The procedure in doing this was essential because they might seem scary at first, but after breaking them down into small steps as indicated above they became doable to me.
Object-oriented Programming summary
Python is designed as an object oriented Programming language. Object-oriented basically means that instead of using the object merely as the carrier of information we put the object at the centre of the process of programming. When we talk about the object-oriented approaches, first we define the objects and how they are related to each other. An object can be all sort of things. It can be a person, a company, an animal, etc. secondly, we add methods to manipulate the data the objects represent.
First of all, new classes need to be introduced to represent the properties the object possesses. For example, we can define a new class called pet which has variables name and age. We can then create an instance called d1 and give the variable name mike and age the value 3. d1 is an object with the name and age of a certain pet.
After a new class is defined, we have to add some methods to manipulate more complicated data. A method is a function contained in the class. The difference is that when using a method we have to refer to an object in the class to which the method is belong first. For example, we can add a method called add_legs for the class pet which adds the number of legs of the pet.
Moreover, we can define a subclass of the parent class which inherites all the features the parent class has. For example, to represent a more detailed object, we can add a subclass dog to the parent class pet to extend the class. Class dog inherits the methods and the variables pet has and in the meantime possesses more methods of its own. We define a subclass instead of defining a new independent class dog and getting rid of the class pet mainly for two reasons. One is that we want to keep the parent class just in case I want a generic pet that is not a dog. Two, I can add more kinds of pets later in the parent class pet like fish or cat and they can also inherits the features of a general pet.
Above is a quick introduction of object-oriented programming.
First of all, new classes need to be introduced to represent the properties the object possesses. For example, we can define a new class called pet which has variables name and age. We can then create an instance called d1 and give the variable name mike and age the value 3. d1 is an object with the name and age of a certain pet.
After a new class is defined, we have to add some methods to manipulate more complicated data. A method is a function contained in the class. The difference is that when using a method we have to refer to an object in the class to which the method is belong first. For example, we can add a method called add_legs for the class pet which adds the number of legs of the pet.
Moreover, we can define a subclass of the parent class which inherites all the features the parent class has. For example, to represent a more detailed object, we can add a subclass dog to the parent class pet to extend the class. Class dog inherits the methods and the variables pet has and in the meantime possesses more methods of its own. We define a subclass instead of defining a new independent class dog and getting rid of the class pet mainly for two reasons. One is that we want to keep the parent class just in case I want a generic pet that is not a dog. Two, I can add more kinds of pets later in the parent class pet like fish or cat and they can also inherits the features of a general pet.
Above is a quick introduction of object-oriented programming.
Subscribe to:
Posts (Atom)