2014年2月23日星期日

Week 6

Week 6
This week’s lab was pretty easy. I finished all the tasks in 40 minutes! The first task of the lab was writing four functions, which can be easily done using for loops. And the second task is to writing test cases in unittest.

And the binary tree is actually what we did in last week’s lab!

2014年2月18日星期二

Week 5

Week 5
In this week’s lab, we traced some recursive functions. I think this tracing process helps me understand how the recursive function actually works.

About the tower of Hanoi recursion:
In a 3-stool Hanoi model, the basic recursion process is:
    def threemove(n, source, temp, destination):
        if n == 1:
            model.move(source, destination)
        elif n > 1:
            threemove(n - 1, source, destination, temp)
            threemove(1, source, temp, destination)
            threemove(n - 1, temp, source, destination)

This is not too hard to understand. I traced this code in a 2-cheese and 3-cheese case and it’s all clear. And in a1, we have a 4-stool Hanoi model, the best approach to move all the cheeses from one stool to another stool also uses recursion. To move 10 cheeses, our code takes 49 steps; 15 cheeses, 129 steps. It’s considerably less than , so I think we got that right!

2014年2月17日星期一

Week 4

Week 4
In this week’s lab, we designed classes motorized and human powered. The main task of this lab is to practice implementing classes, using inheritance and writing test cases. The tasks are pretty easy but the idea is very important. In my opinion, inheritance is the practice of passing on the feature to a child upon a parent. By using inheritance, we can write a new class quickly and it has its parent class’s methods. And it must be noted that choosing test cases is a difficult job, because you have to design some special cases, like an empty collection, a collection with 1 item, smallest interesting case and collection with several items.

For the recursive functions of nested list, I had some a little bit difficulty writing this piece of code:
def rec_max(L):
    return max( # the maximum of
       
        # list of maximums of sublists or plain numbers in L
        [rec_max(x) if isinstance(x, list) else x for x in L])

I can only write a version that is not so succinct:
def rec_max(L):
    result=[]
    for x in L:
        if isinstance(x, list):
            result.append(rec_max(x))
        else:
            result.append(x)
    return max(result)

They basically mean the same thing.

About the permutation function:

The basic idea is to let every element in the string be the first letter and perm(other letters). Fox example: perm(“abc”) = [“a”+perm(“b”+”c”)]+ [“b”+perm(“a”+”c”)]+ [“c”+perm(“a”+”b”)]

Notice that the base case is perm(“a”)=”a”