Discuss / Python / 交作业

交作业

Topic source

from functools import reduce
def normalize(x): return x.capitalize() def fn(x, y): return x * 10 + y L1 = ['adam', 'LISA', 'barT'] L2 = list(map(normalize, L1)) print(L2)

def prod(t): def step1(x,y): return xy return reduce(step1,t) print('3 5 7 9 =', prod([3, 5, 7, 9]))

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): flag=0 num=0 L3=list(map(char2num, s)) for x in L3: if flag==0: num=num+1 if not x=='.': L4.append(x) if x=='.': flag=1 n=reduce(fn,L4) n=n/10**(len(L4)-num+1) return n L4=[] print('str2float(\'123.456\') =', str2float('123.456'))


  • 1

Reply