Saturday, 4 April 2015

Last impression (week #12)

Although it sounds like a cliche, I still have to say: how time flies! It approaches the end of the semester and this is my last slog.

This time, I will look back to the whole course and write the final summary.

In general, we spent around 2/3 of our work and time on recursion. So I guess recursion is the most important thing in this course.

Also, almost every code we wrote were based on classes. So object oriented programming is another important concept we should acquire.

At the beginning, we learned how to trace the recursions and wrote some simple recursive code. And then we are introduced by a new concept: Abstract Data Type, which includes stack, queue and tree. And then we went to tree, binary tree, binary search tree and linked lists, which are all ADT, and applied recursion on them(on almost every method and function in them).

The last thing we learned are assertion and big-Oh. For me, so far, they are not hard.
Maybe it's because we didn't go very deep and I learned big-Oh in CSC165.

To be honest, I was a typical art student in high school and my science courses sucked. The reason why I took computer science courses is that these courses are hard and I want to challenge myself to see how well I can do. I took CSC108 last semester, it was just an introduction to python and we just learned some syntax of python, but it was really hard for me. I didn't know how to do the assignments and wanted to give up, but finally survived. When I take this course, CSC148, although it's harder than CSC108, it gives me the interest to computer science. I still remember how excited I was when I first know the list comprehension. One line code replaces what may be written in many lines! In the first assignment, we made a game(which was I always dreamed)! And the assignment was totally depended on us -- no provided docstrings, no type contracts, and no must-have functions. We decide almost everything by ourselves (as long as we follow the instruction of the handout).

In assignment 2, we developed a strategy for the computer -- minimax. It was the hardest thing in the course I think. We spent around 30 hours on this assignment and finished it in the last minute. I even had nightmares about it! It was brain-draining, but I kind of enjoy diving into it. Finally, the computer in the game becomes so strong that never loses. How magic it is! Computer science is of so much fun, because of this course, a former art student decided to be a computer science student!

I put much effort on this course, and get what I deserve.

 Thank you so much for Danny and Diane, my partners, and my classmates!

Sunday, 29 March 2015

Revisiting earlier posts (week#11)

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!

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.
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:



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.

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.




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.



Sunday, 15 February 2015

Object-oriented programming concepts (week#6)

Object-oriented programming(a.k.a. OOP) is a programming paradigm based on 'object'. It's just like a model of the objects in the real world. We generalize and abstract the real-world objects, and then define them in classes using attributes and methods.

In our daily life, every object has its properties(states) and behaviors (for example, dogs have properties having claws and fur, and they have behaviors bark, eat and run), so does the 'object' in OOP. Basically, it has two main concepts(components):

1. Attributes(Properties)
2. Methods(Behaviors)

These two components are usually presented in the module of the objects -- class

As is taught in the lectures, when we see a handout for coding using python (a typical object-oriented language), we firstly pick up all the verbs and nouns, and then analyze them -- the verbs as methods and nouns as attributes.

Similarly, the first step in OOP is to identify the objects we want to manipulate and analyze how they are related to each other. Once an object has been identified, we generalize it as a class of objects.

Objects are instances of classes, and classes determine the type of objects. To be more specific, let's see the dog example I mentioned before:

Name of the class: Dog
Attributes: claws, fur
Methods:   bark, eat, run




In addition, OOP has a very important and useful property, that is, inheritance.

Inheritance makes it possible to define subclasses of data objects that share some or all of the main class characteristics without rewriting them.

Let's continue the dog example:

Subclass of the class Dog:

Name of the class: PomeranianDog
Attributes: small claws, golden fur, smart
Methods:   bark, eat, run, play

In the example, the Pomeranian is also a dog, and it has all the characteristics of dogs, so it inherits many things from its superclass (Dog) such as attributes claws, fur and methods bark, eat and run. However, since it is a specific kind of dog, it has more detailed things than a dog, which includes small claws, golden fur, smart and play. A subclass inherits some(or all) attributes and methods from the subclass and then extents or modifies the attributes and behaviors.

Inheritance provides OOP many benefits:

1. A programmer can write a subclass inheriting the common features from the superclass instead of changing the module or rewriting all of the code, which makes object-oriented programs easier to modify as well as saves a lot of time and effort.
2. It is reusable not only by the program for which it is initially created but also by other object-oriented programs, so that it is easier to distribute for use in networks.
3. It uses encapsulation(modular objects have their own data and are responsible for their own behaviors), which means changes in one object do not affect other objects, providing greater system security and avoids unintended data corruption.


references:
1. Celina's SLOG: My summary of object-oriented programming concepts
http://celinajiangslog148.blogspot.ca/2015/02/week-6-my-summary-of-object-oriented.html

2. Wikipedia: object-oriented programming
http://en.wikipedia.org/wiki/Object-oriented_programming

3. Margaret Rouse: object-oriented programming
http://searchsoa.techtarget.com/definition/object-oriented-programming