Discuss / Python / 第三题

第三题

Topic source

Tiko_T

#1 Created at ... [Delete] [Delete and Lock User]
DIGITS = dict((chr(ord('0') + val), val) for val in range(10)) # '0': 0, '1': 1
def str2float(s):
    loc = s.find('.')
    def char2num(s):
        return DIGITS[s]
    part1 = reduce(lambda x, y: x * 10 + y, map(char2num, s[:loc]))
    part2 = reduce(lambda x, y: x / 10 + y, map(char2num, s[-1:loc:-1]))
    return part1 + part2/10

  • 1

Reply