Discuss / Python / 作业

作业

Topic source

乔洪宝

#1 Created at ... [Delete] [Delete and Lock User]
from functools import reduce
# 第一题
def normalize(name):
    return reduce(lambda x, y: x + y.lower(), name.upper())
# 第二题
def prod(L):
    return reduce(lambda x, y: x * y, L)
# 第三题
def str2float(s):
    DIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
    return reduce(lambda x, y: x * 10 + y, map(lambda z: DIGITS[z], s.replace('.', ''))) / 10 ** s.index('.')

乔洪宝

#2 Created at ... [Delete] [Delete and Lock User]
from functools import reduce
# 第三题订正(s.index('.')处有误,虽然结果恰好一样)
def str2float(s):
    DIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
    return reduce(lambda x, y: x * 10 + y, map(lambda z: DIGITS[z], s.replace('.', ''))) / 10 ** (len(s) - s.index('.') - 1)


  • 1

Reply