Discuss / Python / test

第一题

def normalize(name): return name[0].upper() + name[1:].lower()

L1 = ['aDam', 'LiSSa', 'barT'] L2 = list(map(normalize, L1)) print(L2)

第二题

from functools import reduce

def mul(x, y): return sum([x, y])

def prod(L): return reduce(mul, L)

print(prod([1,3,5,7,9]))

第三题

def str2float(s): s = s.split('.') def f1(x, y): return x * 10 + y def f2(x,y): return x / 10 + y def str2num(str): L = { '0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9 } return L[str] return reduce(f1, map(str2num, s[0])) + reduce(f2, list(map(str2num, s[1]))[::-1]) / 10

print(str2float('123.456'))


  • 1

Reply