Discuss / Python / 交作业

交作业

Topic source
#_*_coding:utf-8_*_
from _functools import reduce

#第一题
l=['adam', 'LISA', 'barT']
def normalize(name):
    def f(s):
        return s[0].upper()+s[1:].lower()
    return map(f,name)
print(list(normalize(l)))

#第二题
def prod(l):
    return reduce(lambda x,y:x*y,l)
print('3 * 5 * 7 * 9 =', prod([3, 5, 7, 9]))

#第三题
def str2float(s):  
    def char2num(s):
        return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[s]
    def str2numb(l):
        return reduce(lambda x,y:x*10+y,(map(char2num,l)))
    l=s.split('.')
    return str2numb(l[0])+str2numb(l[1])/10**len(l[1])
print('str2float(\'1357.4576\') =', str2float('1357.4576'))

  • 1

Reply