Discuss / Python / 第三题

第三题

Topic source

雷锋20110

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

DIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}

def char2num(s):
    return DIGITS[s]

def str2int(s):
    return reduce(lambda x, y: x * 10 + y, map(char2num, s))

def str2dbl(s):
    return str2int(s) * (10**(-len(s)))

x,y = s.split('.')
return str2int(x) + str2dbl(y)

  • 1

Reply