Discuss / Python / 第一题

第一题

Topic source

Snowood123

#1 Created at ... [Delete] [Delete and Lock User]
def normalize(name):
     L = ""
     n = 1
     for i in name:
         if(n == 1):
             L=L+i.upper()
         else:
             L=L+i.lower()
         n = n + 1
     print(L)
     return L

Snowood123

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

第二题

def prod(s):
    def multi(a,b):
        return a*b
    return(reduce(multi,s))

Snowood123

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

第一题改进

def normalize(name): return (name[:1].upper()+name[1:].lower())

Snowood123

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

第三题

def str2float( s ) :
    s1,s2 = s.split('.')
    x = s1 + s2
    def f(a,b) :
        return 10 * a + b
    def char2int(m) :
        return {'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9}[m]
    return (reduce(f,map(char2int,x))/(10**len(s2)))

  • 1

Reply