Discuss / Python / 作业

作业

Topic source

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

第一题:

def normalize(x):    def case(x):        return x.title()    return list(map(case,x))

第二题:

from functools import reducedef prod(L):    def times(x,y):        product = x*y        return product    return reduce(times,L)

使用lambda简写:

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

第三题:

from functools import reduceDIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}def fn(x,y):    return x*10+ydef char2num(s):    return DIGITS[s]def str2float(s):    if s.index('.'):        point = s.index('.')        s1 = s[:point]        s2 = s[point+1:]        res = reduce(fn,map(char2num,s1))+reduce(fn,map(char2num,s2))/10**len(s2)    return res

  • 1

Reply