2015年3月28日星期六

SLOG 9

This week I think I have to look back and see if I agree with my early ideas. I visited SLOG 6and found that my comprehension on the depth of a node was totally wrong. It should be counted from the root to the node but not from the node to the leaf. Also, I visited SLOG3, and I think I have to supplement something on it. In SLOG3 I talked about the concept of Recursion, but I didn’t mention the way to write a recursion. There are quite a lot of ways to write recursion, and we have to decide which one to use. I can just make some examples to lead.
Suppose we have a nested list [3[567],[2[1]],2], and we want to sum the data inside. By calculating we can easily get the answer, but it is not working for a computer. So we need to write codes by using recursion. One of the skills to write a correct recursion is to find the final thing we operate. In this example, we can tell that those numbers are the so-called final things. So we start the code like this:

def   nested_list(L):
     if isinstance(L,int):
         return L

Then we have to consider about the core of the problem: the way to call the function itself.
If we call [nested_list(x) for x in L] for just once, it means we check if 3[567],[2, [1]], 2 are int. If true, then put it into a blank list. If we call it twice, then 356722 are in the list, and there will only be a list [1] still not there. Call it again, we have all the things in the list. All we have to do here is to sum the list. So we can write a code like this:
return sum ([nested_list(x) for x in L]).
To make it clearer, I put the codes together:

def   nested_list(L):
     if isinstance(L,int):
         return L
else:
    return sum ([nested_list(x) for x in L])




I also read the slogs written by some of my classmates, and their idea helps me a lot. Plenty of my classmates are puzzled on testing as well, and I found that we all struggled about the Minimax of the assignment2. Object Oriented Design is not a common problem, since it is the base of CSC148. But I think the easier it is, the more careful we should be. The easiest part is always the part which makes most people careless.

2015年3月21日星期六

SLOG 8

This week is about testing. It is a really tough and complicated, and I am still very confused about it so that I cannot tell too much on it.
To start with testing, we have to know the reason we test. We test because we don’t know if our codes are running as we expected. So we have to input the expected results and see if it is same as the real answer.
To explain it better, I will make a general example, or we can call it the model.
Firstly, we write ‘ import unittest’ on the top.
Then we create a class, for instance, class TEST, with (unittest.Testcase) after it.
Then we write method.
The first method will be   def setup(self). In this function, all the codes will be operated before every testcase starts. We can initialize all the classed and variables here, and to access in other methods, all the initialized data are supposed to start with self., or be global variables.

The second method should be   def tearDown(self).   Similarly, all codes in this function will be operated before the every testcase starts, but the difference is, they will also be operated after the testcase stops. Since in the testcase, the variables starts with self. that we defined or other global variables will probably be modified by testcase for testing the results, we use tearDown function to make them nothing( self.blahblah = None), like throwing trash.

Finally, we write the key part of the test. We need a method called def test_test_case_name(self), which is allowed to be named by ourselves. Under this method, we need to write four lines of code: Actual = ______, which is the content we test
                               Expected = _______, which is the result we expected
                               Error = _____, which is a hint to let us know that there is an error
                               assert actual == expected, error  # maybe >= or <=


Above are my thoughts about test, maybe now it is still childish, but I will try to learn it

better. 

2015年3月14日星期六

SLOG 7

This week we had our midterm exam, and we spent some lecture time doing some 
review.The new things we learnt this week is the Binary Search Tree. To simplify, I will call 
it BST and the Binary Tree BT.
The only difference between BST and BT is that there is a rule in BST : left child <node< 
right child. Since three is an order among nodes in BST, it will be an extra time for a BST to 
keep its property when doing insert and delete. Also, based on the property, we can exactly 
know whether it goes left or right. Through if statement, we can control the direction of 
Recursive steps.

For example, I would show the way I write method on BST.

def insert (node, data):
      return_node = node
      if not node : 
                     return_node = BTNode(data)
      elif  data < node.data:
                     node.left = insert(node.left, data)
      elif  data > node.data:
                     node.right = insert(node.right, data)
      
      return return_node



Above is just my way of write method insert, and there may also be some other ways. The 
most vital thing is that we create BST to search data more conveniently, so we should not 
make the method very complicated. 

2015年3月7日星期六

SLOG 6

This week we learnt an interesting new thing called tree. Tree looks like this:
Thumbnail for version as of 18:57, 16 February 2011
We can utilize this picture to analysis the concept of Tree. On the top, we see a node (2) with no parent but with children. This is a root. At the bottom, we can find nodes(5,11,4) with no children, and they are called leaves. A subtree is defined as trees that are formed by any tree node together with its descendants and the edges leading to them.
We are also given the concept height, depth and arity, branching factor. 
In this picture, the longest path is 2----5----9----4(or 2----7-----6----5 or 2----7-----6----11), so the height of this tree is 4. The height of a node, for instance, 6,is 3,since the maximum path to the root is 6----7----2.
The depth of a node means the height of the entire tree minus the height of a node. For example, the depth of 6 is 1,since the height of the entire tree is 4,and its own height, as we mentioned above, is 3.
Artiry, branching factor is an interesting concept. It means the maximum number of children. It is a kind of restriction. Sometimes that will be more restrictive: sometimes the right children must be larger than the left children.

This week we also learnt things about LinkedList. Basically, it is a connected list, which is connected node by node. Simply, it can be considered as a tree whose branching factor is 1. But it is not that simple as it looks like.
Generally, LinkedList is expressed in this way: 3 ->2->1->0|, which means the list [3,[2,[1,[0] ] ] ]. There are some relationship among the different parts in this list.
Wrapper class is very important for LinkedList, because LinkedList is list and we have to know its size. Size is not supposed to be in an individual node, and wrapper class can help us find the size since it can turn a single node into the whole list. Usually, we have the information of the front list and back list in a LinkedList. (front: LLNode -- front of list
back: LLNode -- back of list)
We also mentioned the method to iterate a LinkedList. There are two ways to realize it, which are, respectively, while loop and recursion. While loop is what we learnt in CSC108, and we have to focus on recursion in this course.

Here is some methods that I conclude to solve this recursion problem: No matter what we do to the LinkedList, for instance, append or delete, we have to keep an eye on its front and back Node. When appending, we have to record the Node one space ahead the Node that we are operating now. When deleting, we only need to connect the front node and the back node.

This week the assignment 2 is out as well, and my partner and I are trying to solve the problems. We are now confused about how to express the rules of the game by codes.