Discuss / Python / 交作业--MapReduce

交作业--MapReduce

Topic source

安迪博德

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

第一题 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'))

安迪博德

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

可恶的编辑器,重贴第三题

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'))

第一个运行时为什么显示'barT' is not find


  • 1

Reply