第三题
from functools import reduce digits = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
def str2num(s): return digits[s]
def func(x,y): return x*10+y
def str2float(s): t = s.partition('.') ints = float(reduce(func,map(str2num,t[0]))) floats = float(reduce(func,map(str2num,t[-1]))/(10**len(t[-1]))) return ints+floats
Sign in to make a reply
风致
第三题
from functools import reduce digits = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
def str2num(s): return digits[s]
def func(x,y): return x*10+y
def str2float(s): t = s.partition('.') ints = float(reduce(func,map(str2num,t[0]))) floats = float(reduce(func,map(str2num,t[-1]))/(10**len(t[-1]))) return ints+floats