Discuss / Python / A3

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

    def f(x, y):

        return 10*x + y

    def s2f(str):   

        return DIGITS[str];

    s_new = ''

    #默认参数s是符合要求的浮点数字符串

    #先寻找小数点

    idx = 0    

    for i, str in enumerate(s):

        if str == '.':

            #去掉小数点'.',拼接成一个整数

            s_new = s[:i] + s[i+1:]

            idx = i + 1

    #使用上面提到的str2float方法

    my_int = reduce(f, map(s2f, s_new))  

    return my_int * pow(0.1, len(s)-idx)


  • 1

Reply