Discuss / Python / 作业

作业

Topic source

Rayn-S

#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 fn(x, y):
        return x * 10 + y
    def char2num(s):
        return DIGITS[s]
    n = 0
    for c in s:
        if c == '.':
            i = len(s) - n -1
            p = n
        n = n + 1
    return reduce(fn, map(char2num, s[:p]+s[-i:])) * (10 ** (-i))

    a = {'1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6}

    i = map(lambda x: a[x], (i for i in s if i!='.'))

    return reduce(lambda x, y: x * 10 + y, i) * 0.001


  • 1

Reply