Discuss / Python / 啊啊啊,为什么这么复杂,你们怎么想到这么简单的哭泣

啊啊啊,为什么这么复杂,你们怎么想到这么简单的哭泣

Topic source

第一题

-- coding: utf-8 --

def normalize(name): n=0 s='' for i in name: if n==0: s+=i.upper() else: s+=i.lower() n+=1 return s

测试:

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

第二题

-- coding: utf-8 --

from functools import reduce

def prod(L):

def fn(x,y):
    return x*y

return reduce(fn,L)

print('3 5 7 * 9 =', prod([3, 5, 7, 9]))

第三题

-- coding: utf-8 --

from functools import reduce

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,'.':-10}[s]
def fn(x,y):
    if y==-10:
        return x
    else:
        return x*10+y
def Z(s):
    n=1
    for i in s: 
        if i=='.':
            break
        n=n+1
    l=len(s)-n
    return (l)
return (reduce(fn,map(char2num,s)))/(10**Z(s))

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


  • 1

Reply