Discuss / Python / 作业

作业

Topic source

Swiftyxy13

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

-- coding: utf-8 --

from functools import reduce from math import pow

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

def char2num(s): return DIGITS[s]

def str2float(s): l = s.split('.') integer = reduce(lambda x, y: x 10 + y, map(char2num, l[0])) decimal = reduce(lambda x, y: x 0.1 + y * 0.1, map(char2num, ['0'] + list(reversed(l[1])))) return integer + decimal

Swiftyxy13

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

少了两个星号:

integer = reduce(lambda x, y: x * 10 + y, map(char2num, l[0])) decimal = reduce(lambda x, y: x * 0.1 + y * 0.1, map(char2num, ['0'] + list(reversed(l[1]))))


  • 1

Reply