Discuss / Python / 第一题 第二题 第三题

第一题 第二题 第三题

Topic source

trend81

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

#第一题 def normalize(name): name1 = name[0].upper() name1 += name[1:].lower() return name1

测试:

L1 = ['adam', 'LISA', 'barT'] L2 = list(map(normalize, L1)) print(L2)

#第二题 def prod(L): def prduc(x, y): return x * y

return reduce(prduc, L)

print('3 5 7 * 9 =', prod([3, 5, 7, 9])) if prod([3, 5, 7, 9]) == 945: print('测试成功!') else: print('测试失败!')

#第三题 def str2float(s): def char2num(s): return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[s]

def fn(x, y):
    return 10 * x + y

index = s.find('.')
if index != -1:
    s = s[:index] + s[index + 1:]

n = len(s) - index
if index != -1:
    return reduce(fn, map(char2num, s)) * 10 ** (-n)
return reduce(fn, map(char2num, s))

print('str2float(\'123.456\') =', str2float('123.456'))

if abs(str2float('123.456') - 123.456) < 0.00001: print('测试成功!') else: print('测试失败!')


  • 1

Reply