Discuss / Python / 第三题

第三题

Topic source

遥望君山

#1 Created at ... [Delete] [Delete and Lock User]
def str2float(s):
    DIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
    global point
    point = False

    def char2num(s):
        return DIGITS.get(s)

    def fn(x, y):
        global point
        if not y:
            point = True
            return x
        if not point:
            return x * 10 + y
        point *= .1
        return x + y * point 

    return reduce(fn, map(char2num, s))

  • 1

Reply