Discuss / Python / 第三题解答

第三题解答

Topic source

cherose_bj

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

def str2float(s):
    L = s.split('.')
    F = L[1]
    def fn1(x,y):
        return x*10 + y
    def fn2(x,y):
        return x/10 + 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,}[s]
    return reduce(fn1, map(char2num,L[0])) + 0.1*reduce(fn2,map(char2num,F[::-1]))    

print(str2float('12378.4569909'))在此插入代码

LucienEvans

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

F[::-1]真是机智啊!


  • 1

Reply