Discuss / Python / 交作业

交作业

Topic source

啊游客

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

from functools import reduce

def normalize(name): l=name[:] return l[:1].upper()+l[1:].lower()

def prod(L): def mul(x1,x2): return x1*x2 return reduce(mul,L)

def str2float(s): def fn1(x,y): return x10+y def fn2(n,m): return n0.1+m def char2num(s): digits={'0':0,'1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9} return digits[s] tem=s[:] flag=True for x in range(0,len(s)): if(tem[x]=='.'): tem1=tem[:x] tem2=tem[x+1:][::-1] flag=False return reduce(fn1,map(char2num,tem1))+reduce(fn2,map(char2num,tem2))*0.1 break if(flag): return reduce(fn1,map(char2num,tem))

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

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

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


  • 1

Reply