Discuss / Python / 20200430

20200430

Topic source

第一题:

# -*- coding: utf-8 -*-

def normalize(name):

      first_upper=name[0].upper()

      n=len(name)

      last_lower=name[1:n].lower()

      return first_upper+last_lower

第二题:

# -*- coding: utf-8 -*-
from functools import reduce

def prod(L):

      def fun(x1,x2):

            return x1*x2

      return reduce(fun,L)

第三题:

# -*- coding: utf-8 -*-
from functools import reduce

def str2float(s):

         fir=s[0]

         i=0

         while fir!='.':fir

                i=i+1

                fir=s[i]

         s_len=len(s)-i-1

         intt=s[:i]    

        deci=s[i+1:len(s)]

        def char2num(ss):

              digits={'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9}

              return digits[ss]

       def ffun(x1,x2):

             return x1*10+x2  

       return  reduce(ffun,map(char2num,intt))+reduce(ffun,map(char2num,deci))*10**(-s_len)


  • 1

Reply