Discuss / Python / 第三题作业答案,这次真是自己弄得

第三题作业答案,这次真是自己弄得

Topic source
在此插入代码

from functools import reduce

def str2float(s): l = s.split('.') t = l[1][::-1] def str2int(s): def fn(x,y): return x*10+y def back(x,y): return x/10 + y/100 def char2num(s): return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[s] return reduce(fn, map(char2num, s)) def strtofloat(s): def back(x,y): return x/10 + y def char2num(s): return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[s] return reduce(back, map(char2num, s)) return str2int(l[0])+strtofloat(t)/10

运行结果: ============= RESTART: C:/Users/跳跳虎/Desktop/Python/str2float.py =============

str2float('156751.1844598451') 156751.1844598451


  • 1

Reply