Discuss / Python / 交作业 2021/1/4:

交作业 2021/1/4:

Topic source

HellPlay

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

1、规范英文名字:

def normalize(name):
    return name.capitalize()


2、求积函数:

def prod(L):
    return reduce(lambda x, y: x*y, L)


3、字符串转数字(包括整型和浮点型)

方法1:

from functools import reduce

def str2float(s):
    strDIC = {'0':0, '1':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9}
    dotID = (s+'.').find('.')
    if dotID < len(s):
        s = s[0:dotID]+s[dotID+1:]
    def str2num(s):
        return strDIC[s]
    return reduce(lambda x,y: x*10+y,map(str2num,s))/(10**(len(s)-dotID))

方法2:

from functools import reduce
def str2float(s):
    dotID = (s+'.').find('.')
    if dotID < len(s):
        s = s[0:dotID]+s[dotID+1:]
    return reduce(lambda x,y: x*10+y,map(int,s))/(10**(len(s)-dotID))


  • 1

Reply