Discuss / Python / 第三题

第三题

Topic source

蓝e随风

#1 Created at ... [Delete] [Delete and Lock User]
def str2float(s):
    def newStr(s):
        n = s.find('.')
        return [s[:n], s[n + 1:]]

    def char2num(s):
        DIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
        return DIGITS[s]

    new_str = newStr(s)
    return reduce(lambda x, y: 10 * x + y, map(char2num, new_str[0]+new_str[1])) / (10 ** len(new_str[1]))

  • 1

Reply