I revisited my earlier posts and basically I agree with what I wrote before.
But as the course goes on, I find myself have a better understanding of what we did before. I would like to extend what I talked about before add something new.
In week #4, I said that I lost marks on assignment 1 because of the special method '__init__', '__str__', '__repr__', and '__eq__'. But now I know how to do it.
First of all, these 4 methods usually have same variables, because the variables they have are the key points in the whole class. For example, in the assignments, the class 'game_state' has a 'p' in its '__init__' method referring to players, because all the games are 2-player games and player is one of the key points in the class.
In addition, I have to mention that, in the '__eq__' method, except for comparing the variables as in the '__init__' method, we also need to compare the type of the inputs, using 'isinstance()' or 'type()', because sometimes the inputs might have the same value but it's not comparing the thing we want.
In week #5, I talked about tracing recursions. I said recursion was very hard and I didn't know how to write recursions. After practicing a lot, I feel much better about it (though it's still kind of hard for me and sometimes takes a lot of time to think of it).
Before writing code about recursion, we'd better think of some examples first.
Think of the base case, which doesn't need recursion, and write the code for the base case using if expression. And then think of a simplest case except for the base case that uses the recursion only once. And then extend it into more complicated cases, and implement the other part of the code(usually using 'elif' or 'else'). I also find that usually we kind of 'use' the base case as the condition of getting to the leaf of the tree(hitting the end of the recursion).
I went to Diane's office hours to talk about recursion and she told me that sometimes our code about recursion seems with no problem and very reasonable, but by the time we run it, it doesn't work well. When it happens, there must be something wrong with the logic, and the best thing is to try to trace it, begging with the base case I mentions in the last paragraph, and we'll find the problem. I think this advice is very helpful.
I would like to give special thanks to some of my classmates. They wrote clear and well-organized slogs and they have very good understanding of what we learned in CSC148.
I read their slogs every week, and learned a lot from what they wrote.
In addition, I had many doubts and much disappointment on the way taking this course because programming is really hard for me. Sometimes I didn't get what we have to know. But when I read the other classmates' slogs, I found that they have same problems with me and I'm not alone, which makes me less panic! Furthermore, they went to office hours, discussed with classmates and went through all the materials or googled them and solved the problems, which encourages me to solve my problems, too.
One more benefit they bring to me is that, as can be seen in my first two slogs, I didn't write much and they are almost all about feelings. I thought that's what the slog requires. But the other students wrote not only their feelings, but also put many key concept we must know on each topic and made summaries of the topics, with pictures and code. Then I realized that slogs are more like a combination of course notes and journal entries, and started doing it as well, which helps improving my writing of slogs.
They are:
Christian (My dear partner): http://christiangarciacsc148.blogspot.ca/
Celina: http://celinajiangslog148.blogspot.ca/
Huan Ling: http://huanlingslog.blogspot.ca/
Justin: http://csc148justinsong.blogspot.ca/
Toniee: http://tonieewtbeageek.blogspot.ca/
Humair: https://humaircsc148slog.wordpress.com/
(I can tell that They will get a good grade and good luck to them!)
Thank you all again!
Sunday, 29 March 2015
My impression of week#9 - BST mutation (week #10)
The topic in week #7 is the binary search tree mutation - deleting and inserting nodes in the binary search tree.
We just had one hour lecture due to the term test #2 and we had to do the lab exercise on our own due to the strike, I'm not very confident about what we learned this week. Actually I have many doubles.
In the lab exercise #8, there were not many things to do. We just need to do some experiment on functions '_insert' and '_delete'.
I'm ok with the function '_insert'. It inserts nodes into the binary search tree in order.
One thing I should have recorded in week#7's SLOG but forgot is the tree traversal. Our prof didn't talk about it very much so I didn't pay attention to it until we had term test #2 this week and had it covered in the test. Actually this concept is very important because for the most of the time when we're writing the code for a binary tree, we use tree traversal. We mainly practice three kinds of tree traversal: post-order, in-order and pre-order. The following pictures clearly express how they work respectively:
We just had one hour lecture due to the term test #2 and we had to do the lab exercise on our own due to the strike, I'm not very confident about what we learned this week. Actually I have many doubles.
In the lab exercise #8, there were not many things to do. We just need to do some experiment on functions '_insert' and '_delete'.
I'm ok with the function '_insert'. It inserts nodes into the binary search tree in order.
The other functions like '_height', 'contains' and '_find_max' are also easy to understand (Basically very similar to what we did in lab#6 with regard to the BTNode).
What confuses me the most is the function '_delete'. I tried it many times on the nodes at the root, at leaves and in the middle. It worked very well. But I read the code many times and tried to trace it on paper but I still don't understand how it works. I'll go to the office hours for help the next week.
Another difficulty is that I don't understand why we need the class BST. I think we just need to write a class called 'BSTNode' with all the method and functions for BTNode and for BST. I'll ask this question in the office hours, too.
One thing I should have recorded in week#7's SLOG but forgot is the tree traversal. Our prof didn't talk about it very much so I didn't pay attention to it until we had term test #2 this week and had it covered in the test. Actually this concept is very important because for the most of the time when we're writing the code for a binary tree, we use tree traversal. We mainly practice three kinds of tree traversal: post-order, in-order and pre-order. The following pictures clearly express how they work respectively:
impression of week#8 - linked list (week#9)
Last week, we learned a new data structure -- linked list.
Linked list is a linear sequence of nodes with each node referring to its next node(child).
Firstly, we are introduced LLNode -- linked list node. LLNodes are the nodes in a linked list. The following is the code for its init method. As is mentioned before, each node in the linked list has a value(self.value) and refers to a child(self.nxt).
And then it comes to LinkedList:
After practicing it in the lab, I find that the greatest benefit of the linked list is that it's easy to insert and delete nodes. We can change the node anywhere we like without shifting the other nodes.
Linked list is a linear sequence of nodes with each node referring to its next node(child).
Firstly, we are introduced LLNode -- linked list node. LLNodes are the nodes in a linked list. The following is the code for its init method. As is mentioned before, each node in the linked list has a value(self.value) and refers to a child(self.nxt).
Linked list has front(self.front) referring to the first node, and a back(self.back) referring the last node. The size is the number of nodes in it.
Interestingly, as can be seen in the code of the __str__ method, it doesn't "return all nodes", instead, it just returns the "front", which confused me a lot at the beginning. Isn't it returning just one node at the front? Is there anything wrong with the prof's code? And then I tried printing it in wing -- it returns the whole linked list! What a surprise! So I continued to print the front, and get the same result. I didn't understand it until I read Huan Ling's SLOG. Let's recall the concept of linked list node first: It's not just a node with a value, but also a reference to it's child -- the next LLNode, namely, the "front" is not just a single node, it also refers to its next node. The next node also refers to its next node, and so on. That's why when I print front, I get the expression of the whole node.
The last thing to note is that, before writing code for the linked list, it's super useful to draw pictures first and keep track of what's going on, since it's very easy to get lost and mess up when we write the code.
Monday, 16 March 2015
Impression of week #7 - binary tree & binary search tree (week#8)
In week #7, we continued talking about tree, and it's mainly about binary tree and binary search tree.
As for binary tree, the most important property is that every node in it has at most 2 children -- the left child and the right child.
Here is the code of its init method:
And then we practiced a lot about binary search tree in the lab.
A binary search tree is also a binary tree, but I'd like to call it a 'sorted' binary tree: for each node of BST, its value is greater than all values in its left subtree and less than all values in its right subtree, which means, any subtree of a BST is also a BST.
The init method for binary tree and binary search tree are the same. But when we're creating a binary search tree, we place the values(nodes) in order. What we did so far is to use an insert function:
The 'sorted' property of binary search tree is very interesting. It makes lookup operations very convenient and efficient (I guess it also helps other operations a lot, but we just did lookup operation in courses and in the lab, so I will talk about it only this time).
Let's take a look of an example of binary search tree comparing with binary tree:
When we're checking whether a binary tree contains a value, we write the following code:
It uses preorder to go through every node from the root until it finds the value.
But in a binary search tree, if we want to check if it contains a value, we can take advantage of its order and check one side(left or right), ignoring the other on each node, which saves time.
Honestly, recursion was very hard for me and I didn't do very well in the lab. After looking at the solutions, I kind of know how they work, but not that sure. So I traced them in python visualizer, and then wrote down the recursive steps on paper. Now I have a better understanding.
What the function list_longest_ path does is to list the data in a longest path of node.
I gave a tree like this as an example:
It goes through the BST in postorder. FIrst of all, it goes to the root with value 8 and then go down along the left path until it gets to the node with value 2's left child (which is None), and then go to the right child of 2, which is 6, and then continue going to 6's left child and right child (which are both None). Up until now, the left_list and the right_list are empty lists. And then it goes to the node with value 6 (node_data), and assign [6] to the right_list of 2. Next, it goes to node_data in node with value 2, and add it to the right_list [6](since the length of right list is longer), getting a new list [2, 6], and assign it to the right_list of node with value 8. It follows this pattern and finally get a list [8, 2, 6], which is a list of the data of the longest node.
Sunday, 1 March 2015
Abstract data types (week#7)
From wikipedia, 'An abstract data type (a.k.a. ADT) is a mathematical model for a certain class of data structure of one or more programming languages that have similar semantics.' It focuses on semantics, which is the meaning of the object, and operations on the data instead of how to implement it in detail. It doesn't care about which programming language to use.
There are two benefits of ADT, from our course notes:
1. We can think about the algorithms and recipes freely without including all the details of how our objects are implemented.
2. Details of how objects and their components are stored and accessed vary between programming languages, whereas a very good algorithm can be translated into any programming language.
So far, we have learned 3 ADTs. They are stack, queue and tree. I'll give a brief summaries of them next.
1. stack:
A stack is used to store data. There are 3 basic operations on stack:
pop: remove and return the top item on the stack(if it's not empty)
push: add an item on the top of the stack
is_empty: check whether the stack is empty
As can be seen in the 3 operations, it pops from the top and adds on the top as well, which is called 'last-in-first-out' (a.k.a. LIFO).
2. queue:
Very similar to stack, queue is also used for storing data, however, the basic operations of queue is different from stack.
enqueue: add an item at the back of the queue
dequeue: remove the item that is in the front of the queue
is_empty: check if there are items in queue
Queue ADT follows the rule 'first-in-first-out' (FIFO), which contradicts to the stacks'.
3. tree:
As the use of stack and queue, tree also stores data, but it stores data in a hierarchical order.
(From the lecture slides)
A tree is a set of nodes with directed edges between some pair of nodes.
An edge connects a parent node to a child node.
Nodes may contain values.
The beginning node without a parent is the root.
A node without children is called leaf.
There are two benefits of ADT, from our course notes:
1. We can think about the algorithms and recipes freely without including all the details of how our objects are implemented.
2. Details of how objects and their components are stored and accessed vary between programming languages, whereas a very good algorithm can be translated into any programming language.
So far, we have learned 3 ADTs. They are stack, queue and tree. I'll give a brief summaries of them next.
1. stack:
A stack is used to store data. There are 3 basic operations on stack:
pop: remove and return the top item on the stack(if it's not empty)
push: add an item on the top of the stack
is_empty: check whether the stack is empty
As can be seen in the 3 operations, it pops from the top and adds on the top as well, which is called 'last-in-first-out' (a.k.a. LIFO).
2. queue:
Very similar to stack, queue is also used for storing data, however, the basic operations of queue is different from stack.
enqueue: add an item at the back of the queue
dequeue: remove the item that is in the front of the queue
is_empty: check if there are items in queue
Queue ADT follows the rule 'first-in-first-out' (FIFO), which contradicts to the stacks'.
3. tree:
As the use of stack and queue, tree also stores data, but it stores data in a hierarchical order.
(From the lecture slides)
A tree is a set of nodes with directed edges between some pair of nodes.
An edge connects a parent node to a child node.
Nodes may contain values.
The beginning node without a parent is the root.
A node without children is called leaf.
Subscribe to:
Posts (Atom)





















