Discuss / Python / homework

homework

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 str2int(s):
        return DIGITS[s]
    
    def f1(s):
        return s.index('.')   #返回  L中'' .'' 的位置
        
    def fn1(x, y):
        return x*10+y
    
    ans = reduce(fn1,map(str2int,(s[:f1(s)]+s[f1(s)+1:])))  #s[:f1(s)]+s[f1(s)+1:]  对原字符串进行切片重组 字符串连接可以用+
    return ans*pow(0.1,(len(s)-1-f1(s)))

  • 1

Reply