Discuss / Python / 交作业了!!

交作业了!!

Topic source
# -*- coding: utf-8 -*-      
# 第一题
def normalize(name):
    return name.lower().capitalize()

# -*- coding: utf-8 -*-
# 第二题
from functools import reduce
def prod(L):
    return reduce(lambda x,y:x*y,L)

# -*- coding: utf-8 -*-
# 第三题,拓展了一下。可转换正数、负数、整数和小数。
from functools import reduce
def str2float(s):
    def strint(a):
          if a=='.':
              return a
          else: 
              return int(a)
    if s[0]=='-': 
        s=s[1:]
        b=-1
    else:
        b=1
    h=list(map(strint,s))
    if '.' in h:
        n=h.index('.')
        res1=reduce(lambda x,y:x*10+y,h[:n])
        res2=reduce(lambda x,y:x/10+y,h[:n:-1])/10
        return b*(res1+res2)
    else:
        res=reduce(lambda x,y:x*10+y,h)
        return b*res

7班蛋得

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

return name.lower().capitalize() 中的lower()函数不用也可以吧

嗯,是的。这里可以把lower()去掉,感谢提醒。


  • 1

Reply