Discuss / Python / 写一下第3题的答案,测试通过

写一下第3题的答案,测试通过

Topic source

def str2float(s): u = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, \'5': 5, '6': 6, '7': 7, '8': 8, '9': 9}

def char2num(s):
    return u[s]

def f1(x, y):
    return x*10+y

t = s.split('.')    # 从小数点处分割成两部分来处理

r1 = reduce(f1, map(char2num, t[0]))    # 先转换成对应部分的数字
r2 = reduce(f1, map(char2num, t[1]))
r = r1 + r2/(10**len(t[1]))    # 将后半部分数字变为小数,并加上前半部分

return r

  • 1

Reply