第三题
Topic source用pow()函数代替了flag函数,代码更简洁
from functools import reduce
def str2float(s):
def str2num(s):
r = {'.':-1, '0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
return r[s]
def fn(x, y):
if x < 0:
return y
if y < 0:
return x
return x * 10 + y
return reduce(fn, map(str2num, s)) / pow(10,s.index('.'))
print('str2float(\'123.456\')=', str2float('123.456'))
- 1
尼玛取个昵称就这么难