Discuss / Python / 作业

作业

Topic source

黎米YARN

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

第一题

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

def normalize(name):
    return name.title()# 测试:
L1 = ['adam', 'LISA', 'barT']
L2 = list(map(normalize, L1))
print(L2)

第二题

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

from functools import reduce

def prod(L):

    def mult(x,y):
        return x*y
    return reduce(mult,L)
print('3 * 5 * 7 * 9 =', prod([3, 5, 7, 9]))

第三题

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

from functools import reduce

def str2float(s):

    float=len(s)-s.index('.')-1
    s=s.replace('.','')
    def chr2num(m):
        return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[m]
    def order(a,b):
        return a*10+b
    return reduce(order,map(chr2num,s))/10**floatprint('str2float(\'123.456\') =', str2float('123.456'))

  • 1

Reply