Discuss / Python / 我的答案

我的答案

Topic source

第一题

def normalize(name):
    str=name.lower()
    str=name.capitalize()
    return str

第二题

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

第三题

from functools import reduce
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 str2float(s):
    news=s.split('.')
    return reduce(lambda x,y:x*10+y,map(char2num,news[0]))+reduce(lambda x,y:x*10+y,map(char2num,news[1]))/pow(10,len(news[1]))

用capitalize的话,还要个毛lower呀。

from functools import reduce 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 str2float(s): news=s.split('.') return reduce(lambda x,y:x10+y,map(char2num,news[0]))+reduce(lambda x,y:x10+y,map(char2num,news[1]))/pow(10,len(news[1]))

上面这第二个return之后紧接的是个字典吧,字典后面紧接着[s],请问这起到什么作用呢?


  • 1

Reply