Discuss / Python / 交作业了

交作业了

Topic source

* coding: UTF-8*

from functools import reduce def str2float(s): DIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9} n=s[::-1].find('.') k=s[:-n-1]+s[-n:] r=reduce(lambda x, y: x10+y, map(lambda c:DIGITS[c], k)) return r/10*n if name=='main': print('str2float(\'123.456\') =', str2float('123.456')) if abs(str2float('123.456')-123.456) < 0.00001: print('测试成功!') else: print('测试失败!')

总是掉了**这个符号


  • 1

Reply