Question 1:
def normalize(name): if not isinstance(name,str): raise TyprError('您输入的数据类型不是字符串类型,请仔细检查奥') part1=name[:1].upper() part2=name[1:].lower() return part1+part2
L1 = ['adam', 'LISA', 'barT'] L2 = list(map(normalize, L1)) print(L2)
Question 2:
from functools import reduce def prod(L): return reduce(lambda a,b:ab,L) print('3 5 7 9 =', prod([3, 5, 7, 9])) if prod([3, 5, 7, 9]) == 945: print('测试成功!') else: print('测试失败!')
Question 3: # -*- coding: utf-8 -*-
from functools import reduce
def str2float(s): d={'1':1,'2':2,'3':3,'.':0,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9} def str2num(s): return d[s] def str2int(x,y): return x10+y def str2dec(x,y): return x/10.0+y int,dec=s.split('.',1) return reduce(str2int,map(str2num,int))+reduce(str2dec,map(str2num,dec[::-1]))0.1print('str2float(\'123.456\') =', str2float('123.456')) if abs(str2float('123.456') - 123.456) < 0.00001: print('测试成功!') else: print('测试失败!')
Sign in to make a reply
郑男6
Question 1:
-- coding: utf-8 --
def normalize(name): if not isinstance(name,str): raise TyprError('您输入的数据类型不是字符串类型,请仔细检查奥') part1=name[:1].upper() part2=name[1:].lower() return part1+part2
测试:
L1 = ['adam', 'LISA', 'barT'] L2 = list(map(normalize, L1)) print(L2)
Question 2:
-- coding: utf-8 --
from functools import reduce def prod(L): return reduce(lambda a,b:ab,L) print('3 5 7 9 =', prod([3, 5, 7, 9])) if prod([3, 5, 7, 9]) == 945: print('测试成功!') else: print('测试失败!')
from functools import reduce
def str2float(s): d={'1':1,'2':2,'3':3,'.':0,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9} def str2num(s): return d[s] def str2int(x,y): return x10+y def str2dec(x,y): return x/10.0+y int,dec=s.split('.',1) return reduce(str2int,map(str2num,int))+reduce(str2dec,map(str2num,dec[::-1]))0.1
print('str2float(\'123.456\') =', str2float('123.456')) if abs(str2float('123.456') - 123.456) < 0.00001: print('测试成功!') else: print('测试失败!')