Discuss / Python / 我也来贴一个第三题的方法

我也来贴一个第三题的方法

Topic source

陈沉默默

#1 Created at ... [Delete] [Delete and Lock User]
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]



def str2float(s):
        l = s.split('.')
        f1 = reduce(lambda x,y: x*10+y,map(char2num,l[0]))
        f2 = reduce(lambda x,y: x*0.1+y*0.01,map(char2num,l[1]))
        return f1+f2

Scorpiohms

#2 Created at ... [Delete] [Delete and Lock User]
def char2num(m):
    return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[m]
l=s.split('.')
f1= reduce(lambda x, y: x * 10 + y, map(char2num, l[0]))
f2= reduce(lambda x, y:0.1*x + y, map(char2num, l[1][::-1]))
return f1+f2/10

  • 1

Reply