Discuss / Python / 第三题

第三题

Topic source

GrayAshhh

#1 Created at ... [Delete] [Delete and Lock User]
digit = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
def char2num(x):    return digit[x]
def str2float(s):    try:        x, y = s.split('.', 1)    except ValueError:        return reduce(lambda x, y: 10 * x + y, map(char2num, s))    y = y[::-1]    return reduce(lambda x, y: 10 * x + y, map(char2num, x)) + reduce(lambda x, y: x / 10 + y, map(char2num, y)) / 10

GrayAshhh

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

不太会用这个贴代码欸,换行好像不对,再试试

digit = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}

def char2num(x):
    return digit[x]

def str2float(s):
    try:
        x, y = s.split('.', 1)
    except ValueError:
        return reduce(lambda x, y: 10 * x + y, map(char2num, s))
    y = y[::-1]
    return reduce(lambda x, y: 10 * x + y, map(char2num, x)) + reduce(lambda x, y: x / 10 + y, map(char2num, y)) / 10

  • 1

Reply