Discuss / Python / 测试

测试

Topic source

:)

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

第一题

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

第二题

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

第三题

def str2float(s):
    NUMDICT = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
    idx = s.index('.')
    s1 = s[:idx]
    s2 = s[idx + 1:]
    n1 = reduce(lambda x, y: x * 10 + y, map(lambda x: NUMDICT.get(x), s1))
    n2 = reduce(lambda x, y: x * 10 + y, map(lambda x: NUMDICT.get(x), s2))
    return n1 + n2 * 10 ** -len(s2)

  • 1

Reply