公共和儿媳

Discuss / Python / 第三题作业

第三题作业

Topic source

from functools import reduce

def str2float(s):
    digits = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}

    def char2n(s):

        return digits[s]

    def sub1(x,y):

        return x*10+y

    s_n=s.split('.',1)

    return reduce(sub1,map(char2n,s_n[0]+s_n[1]))/(10**len(s_n[1]))


  • 1

Reply