交作业--MapReduce
Topic source可恶的编辑器,重贴第三题
from functools import reduce
def fn(x, y): return x*10 + y def cvt(s): return {'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9}[s]
def str2float(s): if ('.' in s): idx = s.index('.') s1 = s[:idx] s2 = s[idx+1:] return str2float(s1) + str2float(s2)/(10**idx) else: return reduce(fn,map(cvt,s))
print('str2float(\'123.456\') =', str2float('123.456'))
- 1
安迪博德
第一题 def normalize(name): name = name[0].upper() + name[1:].lower(); return name
L1 = ['adam', 'LISA', 'barT'] L2 = list(map(normalize, L1)) print(L2)
第二题 from functools import reduce def mult(x, y): return x * y; def prod(L): return reduce(mult, L)
print('3 5 7 * 9 =', prod([3, 5, 7, 9]))
第三题 from functools import reduce
def fn(x, y): return x 10 + y def cvt(s): return {'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9}[s] def str2float(s): if ('.' in s): idx = s.index('.') s1 = s[:idx] s2 = s[idx+1:] return str2float(s1) + str2float(s2)/(10*idx) else: return reduce(fn,map(cvt,s))
print('str2float(\'123.456\') =', str2float('123.456'))