2015年4月5日星期日

last impression #week12


With the approach of the final exam, the course " introduction to computer science" almost ends. It's time to get a summary of what we learned in the course and get a whole impression. 
1. object, oriented programming concept
- class

- subclass 
1. declaring that we're extending it... *
class NewClass(OldClass):*
...
2. add methods and attributes to specialize*
3. other methods and attributes are searched for in superclass*
 (* are from Prof Heap's vanilla PPT)
2. Recursion

We spent more than half of the semester to discuss issues with recursion. Recursion is very powerful. We firstly learned how to trace recursion. What we usually do is starting from the simplest example and skipping similar steps to get more complicated cases.
When it comes to writing recursion codes, I found myself struggling for a quite long time. Actually, there is no need to trace the recursion during the time you write the codes. 
I found Christian makes a useful summary on his slog.
Recursive step for a list

return sum([sum_list(x) for x in L]) 

Recursive step for a tree

return sum([leaf_count(child) for child in t.children])

And in the assignment 3, I used a for loop to get the recursion and we even make the turtle recursion.
3. Trees
I leaf: node with no children *

I internal node: node with one or more children*

I subtree: tree formed by any tree node together with its
descendants and the edges leading to them.*

I height: 1 + the maximum path length in a tree. A node also has a height, which is 1 + the maximum path length of the tree rooted at that node*

I depth: Height of the entire tree minus the height of a node is the depth of the node.*

I arty, branching factor: maximum number of children for any node. *
 (So a tree with height 3 and arty 3 has at least 3 nodes)
 (* are from Prof Heap's vanilla PPT)
We even 
4. Tree traversal

Preorder traversal: To traverse a binary tree in Preorder, following operations are carried-out (i) Visit the root, (ii) Traverse the left subtree, and (iii) Traverse the right subtree.
Therefore, the Preorder traversal of the above tree will outputs:
7, 1, 0, 3, 2, 5, 4, 6, 9, 8, 10

Inorder traversal: To traverse a binary tree in Inorder, following operations are carried-out (i) Traverse the left most subtree starting at the left external node, (ii) Visit the root, and (iii) Traverse the right subtree starting at the left external node.
Therefore, the Inorder traversal of the above tree will outputs:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

Postorder traversal: To traverse a binary tree in Postorder, following operations are carried-out (i) Traverse all the left external nodes starting with the left most subtree which is then followed by bubble-up all the internal nodes, (ii) Traverse the right subtree starting at the left external node which is then followed by bubble-up all the internal nodes, and (iii) Visit the root.
Therefore, the Postorder traversal of the above tree will outputs:
0, 2, 4, 6, 5, 3, 1, 8, 10, 9, 7
(things above are from the link http://datastructuresnotes.blogspot.ca/2009/02/binary-tree-traversal-preorder-inorder.html )






2015年3月29日星期日

#week 11 A look back


With the final week approaching, this term is almost over and we learned a variety of methods in csc148. It's worthwhile to take a look back on all the slogs. I find that I improve my skills through writing summaries. 

As what I wrote in my first slog (why should geeks write), writing articles is able to improve our skill of explanation. When it comes to the team work of those assignments, it's crucial that you can explain what you think to your teammates and it will contribute to find a way to solve the complicated problems and complete the assignment more quickly. We always discuss for the assignment and things that are abstract.
I mentioned that I found it rather hard to write recursions on my earlier slogs. After that, I wrote some summaries on recursions after I reviewed the course materials and searched related materials online. And I also make summary on other topics and Whenever I get stuck on writing certain part of codes, I will look back on the related summary. It sometimes helps a lot, although not every time.
I look at others' slogs as well and learned a lot from some outstanding summaries. 
In conclusion, I find that writing slogs not only improves the skill of clear explanation but also improves my academic performance.
Here are some links:
http://questionablecsc146decisions.blogspot.ca 
http://dogthatslogsthroughthefog.blogspot.ca

2015年3月26日星期四

Week 10 #the impression of week 9

This week we looked back at assignment 2 and made some summaries.  
The assignment focuses on two parts, one is minimax, and the other is apply move. At first, our team has no idea about how it works and struggled at writing minimax. We even found our minimax was less smart than the random one, which was rather frustrating. Then we try to draw the picture first and find a way to make the computer be smart.


The minimax plays the game for us. We use recursions to max the possibility to win and if there is no chance to win (such as the other player fills the middle blank in a 3*3 lattice) we want the result to be at least tie. 
After writing the codes, we played the game in 3*3 lattice but we found that if we had not filled the middle blank at first, the computer did not always choose the middle blank which is opposed to what we assumed before. Then we tried to fix this problem but always in vain. Eventually, we find out that if we assume both players are using minimax and the computer is the second player, the computer can't win in this 3*3 lattice even if it fills the middle blank first and in this case the best result is tie.
And we used deep copy in this assignment which was googled online. Besides minimax, other functions are easier to write compared to the minimax. We get all the possible moves and use if to judge whether a certain move is legal. 
I think this is a brilliant practice and a very challenging one.
I was looking at others' slogs and learned something from them.
Here is some links.
http://christiangarciacsc148.blogspot.ca/2015/03/week-10-impressions-of-week-9.html
http://www.mybigfatcsc148slog.blogspot.ca

2015年3月15日星期日

Week 9 Impression of week 8

We focused on the LinkedList these days. LinkedList is a class as a list make up of a value and a sub-list. Or we can think it the other way and consider it as an object with a value and a reference to other similar objects.


It will be much easier to understand the codes if we can draw pictures for LinkedList.
We will find the codes below quite useful for tracing the values of the LinkedList.
cur_node = self.front
while <some condition here...>:
    # do something here...
    cur_node = cur_node.nxt
If the mutation does not only relate to the current nodes but also relate to the previous nodes. (For example, delete_back) We will find the codes below quite useful.
prev_node, cur_node = None, lnk.front
        # walk along until cur_node is lnk.back
        while <some condition>:
            prev_node = cur_node
            cur_node = cur_node.nxt
When we mutate the LinkedList, we will probably change the variables like self.front, self.back, self.size, self.value and self.nxt. What can't be ignored is that it's confusing when we change some variables and the best way to make it less complicated is drawing clear pictures carefully.
When it comes to adding a brand new node, we need to create a new node by writing LLNode(value,nxt) and link it with the previous node and the next node.

2015年3月8日星期日

Impressions of week #7

This week we focus on the BTNodes and Linked lists. These are special classes.  In Professor Danny's ppt, he mentioned that most of the work of special methods is done by the nodes "__repr__", "__str__" and "__eq__".Once these are done for nodes, it's easy to do them for the entire list.
1. BTNodes
 Binary tree code has two children which are in order, the left one and the right one. 
eg: 
b = BTNode(8, BTNode(4, BTNode(2, None, None), BTNode(6, None, None)), BTNode(12, None, BTNode(14, None, None)))
print(b)
        14
    12
8
        6
    4
        2
So when we write some recursions linked with BTNodes, we need to pay much attention to the children. What we usually do is operating the left tree and operating the right tree and combining them. And when it comes to list_between(node, start, end), we do not have to compare all the data in the node because data in left subtree are less than node.data and data in right subtree are more than node.data .
2. LLNode
Linked lists are lists made up of an item (value) and a sub-list (rest). And the objects (nodes) have a value and a reference to other similar objects. 
eg
>>> n = LLNode(5, LLNode(7))
>>> print(n)
5 -> 7 ->|
In order to make a reference to the node and move it along the node, what we usually do is using the codes below:

cur_node = self.front
while <some condition here...>:
    # do something here...
    cur_node = cur_node.nxt
The Linked list nodes can be quite confusing and we shall always draw pictures to make the situations much more clear. And what can't be ignored is that once we change the LinkedList, we need to change last node, former last node, back, size and possibly front.


2015年3月1日星期日

Abstract Data Types and recursion

Abstract Data Type is a certain mathematical model for a certain class of  data structures that have similar behavior. (from the definition on the wikipedia) In Professor Danny's class, Danny has described the common ADTs ---- in CS we recycle our intuition about the outside word as ADTs. We abstract the data and operations, and suppress the implementation. And he divides it to three types: 
1. Sequences of items; can be added, removed, accessed by position. 
2. Specialized list where we only have access to most recently added item.  
3. Collection of items accessed by their associated keys.
(from Danny's ppt)
We have discussed several examples in order to understand it. (Stacks, Queues(in lab), Trees etc)
These weeks we are focusing on class Trees. The Tree has children and values and we named the children who has no child leaves. And the BTNode is a binary Tree code which has left children and right children. When it comes to BTNode's leaves, we have the left children and the right children are both equal to None.
And we combine recursion with the class BTNode which is a bit confusing. Let me make an example.
We recall the function list_longest_path itself to solve the problem. We first consider the easiest two conditions and then we extend it to the complicated conditions.






 
 
 
 

2015年2月15日星期日

Summary of object oriented programming concept (week #6)

Python is an object oriented language. It has class, class variable, data member, function overloading, instance variable, inheritance, instance, instantiation, method, object, operator overloading etc. Actually, what we learn in the course csc148 till now is almost all about the OOP.
Python's heart is object and OOP. In python, the basic elements are called object such as integers, strings and functions and so on. Now I want to focus on the class and inheritance. (That's because I think they are the basic two things in OOP.)
Firstly, classes define what is the common thing that the objects inside it has. We often find a general name for the class and classes correspond to variable types.
- class


 
Secondly, we subclass(extends) a superclass(base class) by *


1. declaring that we're extending it... *
class NewClass(OldClass):*
...
2. add methods and attributes to specialize*
3. other methods and attributes are searched for in superclass*
 (* are from Prof Heap's vanilla PPT)