How to think like a Computer Scientist: 课后习题第六章 6-12

#-------------------------------------------------------------------------------
# Name:        module1
# Purpose:
#
# Author:      penglaixy
#
# Created:     27/07/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 days_in_month(month_name):
    month_day_dict={'January':31,'Feburary':28,'March':31,'April':30,'May':31,'June':30,'July':31,'August':31,'September':30,
    'October':31,'November':30,'December':31}

    if month_day_dict.has_key(month_name):
        return month_day_dict[month_name]

def to_secs(hours,minutes,seconds):
    return int(hours*3600+minutes*60+seconds)

def hours_in(sec_num):
    return sec_num//3600

def minutes_in(sec_num):
    return sec_num%3600/60

def seconds_in(sec_num):
    return sec_num%60

def compare(parm_one, parm_two):
    if parm_one > parm_two:
        return 1
    elif parm_one == parm_two:
        return 0
    else:
        return -1
def hypotenuse(sid_one,sid_two):
    return (sid_one**2+sid_two**2)**0.5

def test_suite():
    '''
    Run the suite of tests for code in this module
    '''
    test(days_in_month('Feburary') == 28)
    test(days_in_month('December') == 31)

    test(to_secs(2,30,10)==9010)
    test(to_secs(2,0,0)==7200)
    test(to_secs(0,2,0)==120)
    test(to_secs(0,0,42)==42)
    test(to_secs(0,-10,10)==-590)

    test(to_secs(2.5,0,10.71)==9010)
    test(to_secs(2.433,0,0)==8758)
    test(hours_in(9010)==2)
    test(minutes_in(9010)==30)
    test(seconds_in(9010)==10)

    test(compare(5,4) == 1)
    test(compare(7,7)==0)
    test(compare(2,3)==-1)
    test(compare(42,1)==1)

    test(hypotenuse(3,4) == 5.0)
    test(hypotenuse(12,5)==13.0)
    test(hypotenuse(24,7)==25.0)
    test(hypotenuse(9,12)==15.0)


def main():
    test_suite()



if __name__ == '__main__':
    main()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值