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 str2float(s): def char2float(s): return DIGITS[s] def fn1(x,y): return x10+y for i in range(len(s)): if s[i]=='.': s1=s[:i] s2=s[i+1:] m=len(s2) L1=reduce(fn1,map(char2float,s1)) L2=reduce(fn1,map(char2float,s2))/(10*m) return L1+L2
Sign in to make a reply
Qihong94
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 str2float(s): def char2float(s): return DIGITS[s] def fn1(x,y): return x10+y for i in range(len(s)): if s[i]=='.': s1=s[:i] s2=s[i+1:] m=len(s2) L1=reduce(fn1,map(char2float,s1)) L2=reduce(fn1,map(char2float,s2))/(10*m) return L1+L2