Discuss / Python / 交作业

交作业

Topic source

湖与海海

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

第一题

def normalize(name):

return name.capitalize()

L1 = ['adam', 'LISA', 'barT']

L2 = list(map(normalize, L1))

print(L2)

第二题

def prod(L):

def multiply(x,y):

return x*y

return reduce(multiply,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):

int,float=s.split(".")

def fn(x,y):

return x*10+y

def char(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]

a=reduce(fn,map(char,int))

b=reduce(fn,map(char,float))

return a+b*(0.1**(len(float)))

print('str2float(\'123.456\') =', str2float('123.456'))

if abs(str2float('123.456') - 123.456) < 0.00001:

print('测试成功!')

else:

print('测试失败!')


  • 1

Reply