How to think like a Computer Scientist: 课后习题第十八章5-8

#-------------------------------------------------------------------------------
# Name:        module1
# Purpose:
#
# Author:      penglaixy
#
# Created:     19/09/2013
# Copyright:   (c) penglaixy 2013
# Licence:     <your licence>
#-------------------------------------------------------------------------------
import sys

def test(did_pass):
    '''''print the result of a test '''
    linenum=sys._getframe(1).f_lineno
    if did_pass:
        msg = "Test at line{0} ok".format(linenum)
    else:
        msg = "Test at line{0} failed".format(linenum)
    print msg

def recursive_min(list_test):
    sm = None
    for vl in list_test:
        if type(vl) == type([]):
            vl = recursive_min(vl)
        if sm == None:
            sm = vl
            continue

        if sm > vl:
            sm = vl
    return sm

def count(target, list_test):
    count_num = 0
    for vl in list_test:
        if type(vl) == type([]):
            count_num += count(target, vl)
        else:
            if target == vl:
                count_num += 1
    return count_num

def flatten(list_test):
    list_flatten = []
    for vl in list_test:
        if type(vl) == type([]):
            list_flatten += flatten(vl)
        else:
            list_flatten.append(vl)
    return list_flatten

def fib(num):
    idx = num
    fib_list = [0, 1]
    old_idx = 0
    pre_idx = 1

    num -= 2

    while num >= 0:
        fib_list.append(fib_list[pre_idx] + fib_list[old_idx])
        old_idx += 1
        pre_idx += 1
        num -= 1

    return fib_list[idx]

def main():
    test(recursive_min([2,9,[1,13],8,6]) == 1)
    test(recursive_min([2,[[100, 1], 90], [10,13], 8, 6]) == 1)
    test(recursive_min([2,[[13,-7], 90], [1,100], 8, 6]) == -7)
    test(recursive_min([[[-13,7], 90], 2, [1, 100], 8, 6]) == -13)

    test(count(2, []) == 0)
    test(count(2,[2, 9, [2,1,13,2], 8, [2, 6]]) == 4)
    test(count(7, [[9, [7,1,13,2], 8], [7,6]]) == 2)
    test(count(15, [[9,[7, 1,13,2], 8], [2,6]]) == 0)
    test(count(5, [[5, [5, [1,5], 5], 5],[5,6]]) == 6)
    test(count('a', [['this',['a', ['thing','a'], 'a'], 'is'],['a','easy']]) == 4)

    test(flatten([2, 9, [2,1,13,2], 8, [2,6]]) == [2,9,2,1,13,2,8,2,6])
    test(flatten([[9, [7,1,13,2], 8], [7,6]]) == [9,7,1,13,2,8,7,6])
    test(flatten([[9, [7,1,13,2], 8], [2,6]]) == [9,7,1,13,2,8,2,6])
    test(flatten([]) == [])
    test(flatten([['this',['a', ['thing'], 'a'], 'is'],['a','easy']]) == [
    'this', 'a', 'thing', 'a', 'is', 'a', 'easy'])

    print 'Caculate fibonacci algorithm and get the 1th number is', fib(1)
    print 'Caculate fibonacci algorithm and get the 5th number is', fib(5)
    print 'Caculate fibonacci algorithm and get the 6th number is', fib(6)
    print 'Caculate fibonacci algorithm and get the 200th number is', fib(200)

if __name__ == '__main__':
    main()
>>> 
*** Remote Interpreter Reinitialized  ***
>>> 
Test at line71 ok
Test at line72 ok
Test at line73 ok
Test at line74 ok
Test at line76 ok
Test at line77 ok
Test at line78 ok
Test at line79 ok
Test at line80 ok
Test at line81 ok
Test at line83 ok
Test at line84 ok
Test at line85 ok
Test at line86 ok
Test at line88 ok
Caculate fibonacci algorithm and get the 1th number is 1
Caculate fibonacci algorithm and get the 5th number is 5
Caculate fibonacci algorithm and get the 6th number is 8
Caculate fibonacci algorithm and get the 200th number is 280571172992510140037611932413038677189525
>>> 


猜你喜欢

转载自blog.csdn.net/penglaixy/article/details/11825537