Discuss / Python / 第三题

第三题

Topic source

richardaaaa

#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 split_str(s): if '.' in s: ss = s.split('.') return (ss[0],ss[1]) return (s,'0')

def char2num(s): return DIGITS[s]

def str2float(s): s1,s2 = split_str(s) f1 = reduce(lambda x, y: x * 10 + y, map(char2num, s1)) f2 = reduce(lambda x, y: x / 10 + y, list(map(char2num, s2))[::-1]+[0]) return f1+f2


  • 1

Reply