Discuss / Python / 交作业

交作业

Topic source

UMeowBing

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

1

from functools import reduce import re def normalize(name): return name.capitalize()

2

def prod(L): def product(x,y): return x*y return reduce(product,L)

3

def str2float(s): def char2num(str): return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[str] def f1(x,y): return x10 + y def f2(m): return m/(10*len(str(m))) [s1,s2] = s.split('.') a = reduce(f1,list(map(char2num, s1))) b = f2(reduce(f1,list(map(char2num, s2)))) return a+b # Iterator

L1 = ['adam', 'LISA', 'bArT'] print(list(map(normalize,L1))) print('3 5 7 * 9 =', prod([3, 5, 7, 9])) print('str2float(\'123.456\') =', str2float('123.456'))

结果: ['Adam', 'Lisa', 'Bart'] 3 5 7 * 9 = 945 str2float('123.456') = 123.456

UMeowBing

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

红色部分少了"*",复制过来的时候出错了


  • 1

Reply