#高阶函数-map/reduce 作业
''''
#作业1
# -*- coding: utf-8 -*-
def normalize(name):
return name[0].upper() + name[1:].lower()
L1 = ['adam', 'LISA', 'barT']
L2 = list(map(normalize, L1))
print(L2)
'''
#作业2
# -*- coding:utf-8 -*-
from functools import reduce
def prod(L):
return reduce(lambda x, y : x * y, L)
print('3 * 5 * 7 * 9 =', prod([3, 5, 7, 9]))
if prod([3, 5, 7, 9]) == 945:
print('测试成功!')
else:
print('测试失败!')
#作业3
def str2float(s):
list1 = s.split('.')
n1 = reduce(lambda x, y: 10 * x + y, map(int, list1[0]))
n2 = reduce(lambda x, y: (x * 0.1 + y) , map(int,list1[1][::-1]))
# 注意list1要倒置,另外一种方法可以在函数返回利用长度乘以0。1乘方
return n1 + n2 * 0.1
print('str2float(\'123.456\') =', str2float('123.456'))
if abs(str2float('123.456') - 123.456) < 0.00001:
Sign in to make a reply
星辰德法
#高阶函数-map/reduce 作业
''''
#作业1
# -*- coding: utf-8 -*-
def normalize(name):
return name[0].upper() + name[1:].lower()
L1 = ['adam', 'LISA', 'barT']
L2 = list(map(normalize, L1))
print(L2)
'''
'''
#作业2
# -*- coding:utf-8 -*-
from functools import reduce
def prod(L):
return reduce(lambda x, y : x * y, L)
print('3 * 5 * 7 * 9 =', prod([3, 5, 7, 9]))
if prod([3, 5, 7, 9]) == 945:
print('测试成功!')
else:
print('测试失败!')
'''
'''
#作业3
# -*- coding: utf-8 -*-
from functools import reduce
def str2float(s):
list1 = s.split('.')
n1 = reduce(lambda x, y: 10 * x + y, map(int, list1[0]))
n2 = reduce(lambda x, y: (x * 0.1 + y) , map(int,list1[1][::-1]))
# 注意list1要倒置,另外一种方法可以在函数返回利用长度乘以0。1乘方
return n1 + n2 * 0.1
print('str2float(\'123.456\') =', str2float('123.456'))
if abs(str2float('123.456') - 123.456) < 0.00001:
print('测试成功!')
else:
print('测试失败!')
'''