Discuss / Python / 纯小白作业

纯小白作业

Topic source

from functools import reduce

  • 第一题 def FirstStrToDX(s): return s[0].upper()+s[1:].lower()

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

  • 第二题 def chengfa(x,y): return xy def prod3(L3): #return reduce(chengfa,L3) return reduce(lambda x,y:xy,L3)

print(prod3([3, 5, 7, 9]))

  • 第三题 def str2float(s): s=s.split('.') if len(s)==1:
      return reduce(lambda x,y:x*10+y,map(int,s[0]))
    
    else:
      return reduce(lambda x,y:x*10+y,map(int,s[0]+s[1]))/pow(10,len(s[1]))
    

print(str2float('123.456789')) print(str2float('0.123')) print(str2float('123.')) print(str2float('123'))


  • 1

Reply