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
>>> 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值