#-------------------------------------------------------------------------------
# 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()
How to think like a Computer Scientist: 课后习题第六章 6-12
最新推荐文章于 2021-10-03 12:08:29 发布