Sunday, February 23, 2014

Week 6: Tree

After the reading week, let me summarize what I learned from Week 6. The central topic of this week is Tree, which combines the concepts of recursion and object-orienting programming. Through Tree, we can create, store and analyze the data by categories. It is a little hard to understand "preorder":
Node is the first, then the left,  the right branch is the last
def print_tree(tree):
    if tree is None: return
    print(tree.cargo, end=" ")
    print_tree(tree.left)
    print_tree(tree.right)
"inorder"
The left branch is the first, then the node,  the right branch is the last
def print_tree_inorder(tree):
    if tree is None: return
    print_tree_inorder(tree.left)
    print(tree.cargo, end=" ")
    print_tree_inorder(tree.right)
and "postorder"
The left branch is the first, then the right,  the node is the last
def print_tree_postorder(tree):
    if tree is None: return
    print_tree_postorder(tree.left)
    print_tree_postorder(tree.right)
    print(tree.cargo, end=" ")
in its traversal as I am used to understanding the tree by preorder which I think a kind of my subconsciousness.
It is very useful in computer as I find that it can be linked with logic analysis - especially for CSC165 !. Every node has two branches as if every assumption has two opposite cases - true or false. We can conclude something from the different situations by using trees.
I have finished the part I of the assignment II. The goal of next week is to win the test!

Monday, February 10, 2014

Week 5: Assignment 1 & names in different scopes

Well, I think this week is relatively relaxing as I have finished the Assignment 1 one week ago. The instructor taught us a kind of iterative method to figure out the Hanoi Tower. I don't use it in my assignment as I have solved it by a non-iterative approach. However, I have to acknowledge that the iteration would be easier in this problem, at least in terms of comprehension. The concept of names in different scopes is easy to understand and very useful. I have used a similar method in my assignment  which is to create a non-local variable in a function to record how to move n cheeses by four stools in order to increase efficiency. Yes, it derives from the last week's reading! I believe these new material will help me solve more tough problems in the future.

Sunday, February 2, 2014

Week 4: recursion in nested list and turtle

This week, we talked about more recursion examples. In this examples, I have learned how to trace the recursion and understand the code of recursion functions. Especially, I learned how to make the function more efficient by using dictionaries in the recursion.
Recursion is an easy way to handle some problems that look complicated but sometimes it will wast some time to operate. In our reading, it will take a lot of time to cope with the problem of Fibonacci numbers. The code is like:
def fib(n):
    if n <= 1:
        return n
    t = fib(n-1) + fib(n-2)
    return t
However, the reading material also tells us how to handle it by dictionaries, we can simply create a dictionary to record what we have already gotten:
alreadyknown = {0: 0, 1: 1}

def fib(n):
    if n not in alreadyknown:
        new_value = fib(n-1) + fib(n-2)
        alreadyknown[n] = new_value
    return alreadyknown[n]
It is amazing and really helpful in my programming the assignment 1. Now, I can effectively accelerate my program by this technique. I find that we need to avoid not only duplicate codes but also allowing the computer to do duplicate tasks in programming. I have noticed that how important and helpful the efficiency is. I know we will cover more about efficiency in the future so this is a good beginning for myself.
After I spent two nights and a few hours in the day, I have successfully achieved the entire tasks of the assignment 1. The most difficult point is processing Tour.py which lets the computer itself to finish the Hanoi problem. It's a long way to encode, debug, mortify the documentation and optimize it. Nevertheless, I really enjoy this process and felt excited when the computer perfectly did what I asked!