# -*- coding: utf-8 -*-
def normalize(name):
return name[:1].upper() + name[1:]
# 测试:
L1 = ['adam', 'LISA', 'barT']
L2 = list(map(normalize, L1))
print(L2)
# -*- coding: utf-8 -*-
from functools import reduce
def multi(x, y):
return x*y
def prod(l):
return reduce(multi, l)
print('3 * 5 * 7 * 9 =', prod([3, 5, 7, 9]))
if prod([3, 5, 7, 9]) == 945:
print('测试成功!')
else:
print('测试失败!')
# -*- coding: utf-8 -*-
from functools import reduce
CHAR_TO_FLOAT = {
'0': 0,
'1': 1,
'2': 2,
'3': 3,
'4': 4,
'5': 5,
'6': 6,
'7': 7,
'8': 8,
'9': 9,
'.': -1
}
def str2float(s):
nums = map(lambda ch: CHAR_TO_FLOAT[ch], s)
point = 0
def to_float(f, n):
nonlocal point
if n == -1:
point = 1
return f
if point == 0:
return f * 10 + n
else:
point = point * 10
return f + n / point
return reduce(to_float, nums, 0.0)
from functools import reduce
def str2float(s):
def fn(x,y):
return x*10+y
n=s.index('.')
s1=list(map(int,[x for x in s[:n]]))
s2=list(map(int,[x for x in s[n+1:]]))
return reduce(fn,s1) + reduce(fn,s2)/10**len(s2)
print('\'123.4567\'=',str2float('123.4567'))
print('str2float(\'123.456\') =', str2float('123.456'))
if abs(str2float('123.456') - 123.456) < 0.00001:
print('测试成功!')
else:
print('测试失败!')
这个答案我觉得 是非常棒的非常能够理解的!如果大家有什么问题的话可以直接评论!