Discuss / Python / 第三题 如有错误 欢迎指正

第三题 如有错误 欢迎指正

Topic source

from functools import reduce

def str2float(x):
    list(x)
    position = x.index('.')
    integer1 = list(map(int, x[:position]))
    integer2 = reduce(lambda x, y: x * 10 + y, integer1)
    decimal1 = list(map(int, x[:position:-1]))
    decimal2 = reduce(lambda x, y: x * 0.1 + y, decimal1)
    return integer2 + 0.1 * decimal2

print('str2float(\'123.456\') =', str2float('123.456'))

-李铁蛋_

#2 Created at ... [Delete] [Delete and Lock User]

情况考虑不全 第一个就跑不通,建议看下参考代码

print(str2float('0'))
print(str2float('123.456'))
print(str2float('123.45600'))
print(str2float('0.1234'))
print(str2float('.1234'))
print(str2float('120.0034'))
print(str2float('.'))

  • 1

Reply