Discuss / Python / 第一题

第一题

Topic source

忽略略略

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

def normalize(name): name = str.title(name[0:]) return name L1 = ['adam', 'LISA', 'barT'] L2 = list(map(normalize, L1)) print(L2)

忽略略略

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

第二题 from functools import reduce

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

忽略略略

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

第三题 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}

def char2num(s): return DIGITS[s]

def str2int(s): return reduce(lambda x, y: x * 10 + y, map(char2num, s))

def str2float(s): return float(s) print('str2float(\'123.456\') =', str2float('123.456')) if abs(str2float('123.456') - 123.456) < 0.00001: print('测试成功!') else: print('测试失败!')


  • 1

Reply