Discuss / Python / 第3题好不容易

第3题好不容易

Topic source

VincyChan4231

#1 Created at ... [Delete] [Delete and Lock User]

你们随随便便就能写出lambda的好流弊,我写下边这些都费了好长时间>_<

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 str2float(s):

    def char2num(ch):

        return DIGITS[ch]

    def fn1(x,y):

        return x*10+y

    def fn2(x,y):

        l=len(str(fn1(x,y)))

        print(fn1(x,y))

        return fn1(x,y)

    dian=s.find('.')

    if dian==-1:

        return reduce(fn1,map(char2num,s))

    else:

        sl=s[0:dian]

        sr=s[dian+1:]

        print(sl,sr)

        return reduce(fn1,map(char2num,sl))+reduce(fn2,map(char2num,sr))/(10**len(sr))

为什么我写这个 s.find('.') 一直报错?


  • 1

Reply