Discuss / Python / 第三题啊啊啊 啊

第三题啊啊啊 啊

Topic source

kkopite

#1 Created at ... [Delete] [Delete and Lock User]
from functools import reduce

a = 0
b = 1

def f(x,y):
    global a  #全局变量
    global b  #全局变量
    if y==0.1:
        a = 1
        return x
    if a:
        #小数点后计算
        b=b*10
        return x + y/b
    return 10*x + y


def char2num(s):
    return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9,'.':0.1}[s]
def str2float(s):
    return reduce(f, map(char2num, s))

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

  • 1

Reply