Discuss / Python / Homework

Homework

Topic source

Ethan-Fanny

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

case 1 def normalize(name): return name[0].upper()+name[1:].lower() L1 = ['adam', 'LISA', 'barT'] L2 = list(map(normalize, L1)) print(L2)

case 2 def prod(L): return reduce(lambda x,y:xy,L) print('3 5 7 9 =', prod([3, 5, 7, 9]))

case 3

CHAR_TO_FLOAT = { '0' : 0, '1' : 1, '2' : 2, '3' : 3, '4' : 4, '5' : 5, '6' : 6, '7' : 7, '8' : 8, '9' : 9, '.' : -1, } def str2float(s): nums = map(lambda ch: CHAR_TO_FLOAT[ch], s) dot = 0 def to_float(f, n): nonlocal dot if n == -1: dot = 1 return f if dot == 0: return f 10 + n else: dot = dot 10 return f + n / dot return reduce(to_float, nums, 0.0)

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


  • 1

Reply