Discuss / Python / 3个练习题答案

3个练习题答案

Topic source

wings

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

贯彻廖老师提到的极简原则,3个例子每个函数都只用一行:

def normalize(s):

    return ''.join([ch.upper() if i==0 else ch.lower() for i,ch in enumerate(s)])

def prod(L):

    return reduce(lambda x,y:x*y,L)

def str2float(s):

    return reduce(lambda x,y:10*x+y,map(lambda x:ord(x)-48,s.split('.')[0]))+reduce(lambda x,y:0.1*x+y,map(lambda x:ord(x)-48,s.split('.')[1][::-1]))/10


  • 1

Reply