Discuss / Python / 作业记录

作业记录

Topic source

官总gg

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

直接分为整数部分和小数部分计算(未考虑没有小数的情况)

 def str2float(s):  
    def char2float(s):
        DIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '.': -1}
        return DIGITS[s]
    def calculate1(x, y):
        return x * 10 + y
    def calculate2(x, y):
        return x * 0.1 + y

    List = list(map(char2float, s))
    num = List.index(-1)
    List[num] = 0 # 找到小数点位置,改写为0
    l1 = List[:num]
    l2 = List[num:] 
    l2.reverse() # 切分列表
    return reduce(calculate1, l1) + reduce(calculate2, l2)

  • 1

Reply