Discuss / Python / 没看评论之前的,我的愚蠢代码55555,都没想到用幂函数

没看评论之前的,我的愚蠢代码55555,都没想到用幂函数

Topic source

SetNM

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

def str2float(s):

    i = s.index('.')

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

    def cheng10(x,y):

        return x * 10 + y

    def chu10(x,y):

        return x * 0.1 + y

    def char2num(s):

        return digits[s]

    s1 = reduce(cheng10,map(char2num,s[:i:]))

    s2 = reduce(chu10,map(char2num,s[i+1::][::-1]))

    return s1 + s2 /10


  • 1

Reply