Discuss / Python / HomeWork03

HomeWork03

Topic source

雲☁

#1 Created at ... [Delete] [Delete and Lock User]
from functools import reducedef str2float(s):    m, n =s.split('.')    d = {'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,          '6':6,'7':7,'8':8,'9':9}    def chr2int(s):        return d[s]    def fn(x,y):        return 10 * x + y    return reduce(fn,map(chr2int,m)) + reduce(fn,map(chr2int,n)) * 0.1**len(n)print('str2float(\'123.456\') =', str2float('123.456'))if abs(str2float('123.456') - 123.456) < 0.00001:    print('测试成功!')else:    print('测试失败!')

雲☁

#2 Created at ... [Delete] [Delete and Lock User]
def str2float(s):    m, n =s.split('.')    def fn(x,y):        return 10 * x + y    return reduce(fn,map(int,m)) + reduce(fn,map(int,n)) * 0.1**len(n)print('str2float(\'123.456\') =', str2float('123.456'))if abs(str2float('123.456') - 123.456) < 0.00001:    print('测试成功!')else:    print('测试失败!')

  • 1

Reply