Discuss / Python / homework

homework

Topic source

gitKong

#1 Created at ... [Delete] [Delete and Lock User]
第一题:

def normalize(name):
    if isinstance(name, Iterable) and len(name) > 0:
        total = name.lower()
        firstC = total[0].upper()
        return firstC + total[1:]

第二题:

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

第三题:

def str2float(s):
    if not isinstance(s, str) or '.' not in s:
        return s
    def countNum(x, y):
        return x * 10 + y
    L = list(map(str, s))
    pointIndex = L.index('.')
    L1 = L[:pointIndex]
    L2 = L[pointIndex + 1:]
    return reduce(countNum, map(int,L1)) + reduce(countNum, map(int,L2)) / 10**len(L2)


  • 1

Reply