Discuss / Python / solution of problem 3

solution of problem 3

Topic source

在路上Ce

#1 Created at ... [Delete] [Delete and Lock User]
# map reducefrom functools import reduce

# self-defined int() funcDIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
def char_to_num(s):
    return  DIGITS[s]

def str_to_int(s):
    return reduce(lambda x, y: x * 10 + y, map(char_to_num, s))

# self-defined float() funcdef str_to_float(s):
    sl = s.split('.')
    sl1 = list(sl[0])
    sl2 = list(sl[1])
    sl2.reverse()

    return reduce(lambda x, y: x * 10 + y, map(char_to_num, sl1)) +\
           0.1 * reduce(lambda x, y: x * 0.1 + y, map(str_to_int, sl2))

在路上Ce

#2 Created at ... [Delete] [Delete and Lock User]
correction
# map reducefrom functools import reduce

# self-defined int() funcDIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
def char_to_num(s):
    return  DIGITS[s]

def str_to_int(s):
    return reduce(lambda x, y: x * 10 + y, map(char_to_num, s))

# self-defined float() funcdef str_to_float(s):
    sl = s.split('.')
    sl1 = list(sl[0])
    sl2 = list(sl[1])
    sl2.reverse()

    return reduce(lambda x, y: x * 10 + y, map(char_to_num, sl1)) +\
           0.1 * reduce(lambda x, y: x * 0.1 + y, map(char_to_num, sl2))

在路上Ce

#3 Created at ... [Delete] [Delete and Lock User]
# map reducefrom functools import reduce

# self-defined int() funcDIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
def char_to_num(s):
    return  DIGITS[s]

def str_to_int(s):
    return reduce(lambda x, y: x * 10 + y, map(char_to_num, s))

# self-defined float() func
def str_to_float(s):
    sl = s.split('.')
    sl1 = list(sl[0])
    sl2 = list(sl[1])
    sl2.reverse()

    return reduce(lambda x, y: x * 10 + y, map(char_to_num, sl1)) +\
           0.1 * reduce(lambda x, y: x * 0.1 + y, map(char_to_num, sl2))

  • 1

Reply