Discuss / Python / 交作业

交作业

Topic source

第一题:

def normalize(name):

    returnname[0].upper()+name[1:len(name)].lower()

第二题:

fromfunctools import reduce

defprod(L):

    def multiply(x,y):

        return x*y

    return reduce (multiply,L)

第三题:

fromfunctools import reduce

defstr2float(s):

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

    def char2num(s):

        return DIGITS[s]

    def fn(x, y):

        return 10 * x + y

    n = s.index('.')

    s1 = list(map(char2num, s[0:n]))

    s2 = list(map(char2num, s[n + 1:]))

    return reduce(fn, s1) + reduce(fn, s2) / 10** len(s2)

厉害


  • 1

Reply