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”

没有评论:

发表评论