from functools import reduce
def normalize0(name):
def capital(str):
return str.capitalize()
return list(map(capital,name))
BIG_SMALL = {'A':'a', 'B':'b', 'C':'c', 'D':'d', 'E':'e', 'F':'f', 'G':'g', 'H':'h', 'I':'i', 'J':'j', 'K':'k', 'L':'l', 'M':'m', 'N':'n', 'O':'o', 'P':'p', 'Q':'q', 'R':'r', 'S':'s', 'T':'t', 'U':'u', 'V':'v', 'W':'w', 'X':'x', 'Y':'y', 'Z':'z'}
SMALL_BIG = {'a':'A', 'b':'B', 'c':'C', 'd':'D', 'e':'E', 'f':'F', 'g':'G', 'h':'H', 'i':'I', 'j':'J', 'k':'K', 'l':'L', 'm':'M', 'n':'N', 'o':'O', 'p':'P', 'q':'Q', 'r':'R', 's':'S', 't':'T', 'u':'U', 'v':'V', 'w':'W', 'x':'X', 'y':'Y', 'z':'Z'}
def normalize1(L1):
L0=[]
for j,name in enumerate(L1):
value=''
for i,x in enumerate(name):
if x in BIG_SMALL:
x=BIG_SMALL[x]
if i==0:
x=SMALL_BIG[x]
value=value+x
L0.append(value)
return L0
def normalize2(name):
return list(map(lambda s:s[:1].upper() + s[1:].lower(), name))
L3 = ['adam', 'LISA', 'barT']
print(normalize0(L3))
def prod0(L):
def mul(x,y):
return x*y
return(reduce(mul,L))
def prod1(L):
return(reduce(lambda x,y:x*y,L))
print('3 * 5 * 7 * 9 =', prod1([3, 5, 7, 9]))
if prod1([3, 5, 7, 9]) == 945:
print('测试成功!')
else:
print('测试失败!')
DIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
def char2num(c):
return DIGITS[c]
def str2float(s):
s = s.split('.')
s1 = s[0]
s2 = s[1:] or ['0']
s2 = s2[0] or '0'
s2 = s2[::-1]
return (reduce(lambda x, y: x * 10 + y, map(char2num, s1))) + (reduce(lambda x, y: x / 10 + y, map(char2num, s2))) / 10
def str2float1(s):
return reduce(lambda x, y: x + y*pow(10,-len(str(y))), map(int, s.split('.')))
print('str2float(\'123.456\') =', str2float('123.456'))
if abs(str2float('123.456') - 123.456) < 0.00001:
print('测试成功!')
else:
print('测试失败!')
参考网站:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/0014317852443934a86aa5bb5ea47fbbd5f35282b331335000#0