Discuss / Python / 交作业

交作业

Topic source

loveprruy

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

练习1:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
def normalize(name):
    return (name.lower()).capitalize()

L1=['adam', 'LISA', 'barT']
L2=list(map(normalize,L1))
print(L2)

练习2:

#!/usr/bin/env python3
# -*- 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]))

练习3

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from functools import reduce
def str2float(s):
    def char2float(keys):
        return {'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9}[keys]
    if isinstance(eval(s), float) :
        s1,s2=s.split('.')
        return reduce(lambda x,y:x*10+y,map(char2float,s1+s2))/10**len(s2)
    else:
        return reduce(lambda x,y:x*10+y,map(char2float,s))
#输出123,123.456和123.0都通过.
print('str2float(\'123.456\') =', str2float('123.456'))

  • 1

Reply