Discuss / Python / 第3题

第3题

Topic source

_Ljj110719

#1 Created at ... [Delete] [Delete and Lock User]
  • @萌萌的小Karl给出了python内置的的s.split()pow(),这样就不用麻烦去拆分字符串blabla了,这里再用lambda函数完成第3题(其实差不多,就是用lambda x, y: x*10+y代替了@萌萌的小Karl的函数f)
def str2float(s):
    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]

    s1, s2 = s.split('.')
    l = lambda x, y: x*10+y
    return reduce(l, map(char2num, s1))+reduce(l, map(char2num, s2))/pow(10, len(s2))

  • 1

Reply