Discuss / Python / 作业3

作业3

Topic source

BJLHZHEN

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

digits = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
def str2float(s):
    def char2num(c):
        return digits[c]
    data = s.split('.')
    integer = reduce(lambda x, y: x * 10 + y, map(char2num, data[0]))
    decimal = reduce(lambda x, y: x * 10 + y, map(char2num, data[1])) * 0.1**len(list(map(char2num, data[1])))
    return integer + decimal

  • 1

Reply