练习3----
Topic source好像复制时出了点问题。。。
from functools import reduce
str2int = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
def str2float(s):
if '.' not in s:
x = s
y = '00' #是否整数
else:
b = s.index('.')
if b == 0:
x = '00'
y = s[-1:b:-1]
else:
x = s[:b]
y = s[-1:b:-1]
x1 = map(lambda s: str2int[s], x)
f1 = reduce(lambda p,q: 10 * p + q, x1)
y1 = map(lambda s: str2int[s], y)
f2 = reduce(lambda m,n: m/10 + n, y1)/10
f = f1 + f2
return f
print(str2float('0'))
print(str2float('123.456'))
print(str2float('123.45600'))
print(str2float('0.1234'))
print(str2float('.1234'))
print(str2float('120.0034'))
def str2float(s):
s1, s2 = s.split('.')
return reduce(lambda x, y: x * 10 + y, map(lambda x : int(x) / (10.0 ** len(s2)), s1 + s2))
- 1
隐于泉边
from functools import reduce
str2int = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
def str2float(s): if '.' not in s: x = s y = '00' #是否整数 else: b = s.index('.') if b == 0: x = '00' y = s[-1:b:-1] else: x = s[:b] y = s[-1:b:-1]
print(str2float('0')) print(str2float('123.456')) print(str2float('123.45600')) print(str2float('0.1234')) print(str2float('.1234')) print(str2float('120.0034')) print(str2float('120.0034'))