Discuss / Python / 作业

作业

Topic source

kissgun

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

作业一

def normalize(name):

    return name[0].upper() + name[1:].lower()

# 测试:

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

L2 = list(map(normalize, L1))

print(L2)

**作业二
**

# -*- 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('测试失败!')

****作业三


# -*- coding: utf-8 -*-

from functools import reduce

def str2float(s):

    idx = s.index('.')

    return reduce(lambda x, y: x * 10 + y, map(int, s[:idx])) + reduce(lambda x, y: x / 10 + y, map(int, s[idx + 1:][::-1]))/10

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

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

    print('测试成功!')

else:

    print('测试失败!')


  • 1

Reply