Discuss / Python / 感觉我写的是最麻烦的 /哭笑

感觉我写的是最麻烦的 /哭笑

Topic source

alex不勉强

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

def str2float(s): def fn(x, y): return x * 10 + y

def char2num(s):
    if s!='.':
        return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[s]

tmpStr = list(map(char2num, s))
i = 0
switch = False
for index,item in enumerate(tmpStr):
    if item ==None:
        switch = True
        tmpStr.remove(item)
    if switch :
        i = i+1

tmpFloat = reduce(fn, tmpStr)
print(tmpFloat)
if i == 0:
    return tmpFloat
else:
    x = 1
    while i>0:
        x = x*10
        i = i-1
    return tmpFloat/x

print('str2float(\'1231.456\') =', str2float('1231.01554'))


  • 1

Reply