Discuss / Python / 三个题,参考完成QAQ

三个题,参考完成QAQ

Topic source
def normalize(name):    return name[0].upper() + name[1:].lower()L1 = ['adam', 'LISA', 'barT']L2 = list(map(normalize, L1))  # map是将L1中的每一个参数拿出来,进入函数中计算print(L2)
from functools import reduce


def prod(L):    def multiply(x, y):        return x * y    return reduce(multiply, L)print('3 * 5 * 7 * 9 =', prod([3, 5, 7, 9]))if prod([3, 5, 7, 9]) == 945:    print('测试成功!')else:    print('测试失败!')
from functools import reduce

DIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}# 先将字符串s按照小数点划分为俩部分# 先将s1、s2 按照整数算好# 再通过s2的长度,将小数点标回(通过除)def str2float(s):    def char2num(i):        return DIGITS[i]    s1, s2 = s.split('.')    num = reduce(lambda x, y: x * 10 + y, map(char2num, s1 + s2))    return num / pow(10, len(s2))print('str2float(\'123.456\') =', str2float('123.456'))if abs(str2float('123.456') - 123.456) < 0.00001:    print('测试成功!')else:    print('测试失败!')

  • 1

Reply