Discuss / Python / 第三题

第三题

Topic source

Nicktimebreak

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

实现是实现了,但是感觉自己做的好烂

from functools import reduce
def str2float(s):
    s=list(s)
    b = len(s) - s.index('.')-1
    s.pop(s.index('.'))
    n = 1
    while b>0:
        n = n*10
        b -=1
    def char2num(s):
       return {'0':0, '1':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9}[s]

    return float(reduce(lambda x, y: x*10 + y, map(char2num,s))/n)

print('str2float(\'12.553456.\')=', str2float('12.553456'))

  • 1

Reply