Discuss / Python / 练习

练习

Topic source

练习一:

def normalize(name):
    return name[0].upper() + ''.join(list(map(lambda s: s.lower(), name[1:])))

练习二:

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

练习三:

def str2float(s):
    DIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
    d = s.index('.')
    i = reduce(lambda x, y: 10 * x + y, list(map(lambda x: DIGITS[x], s[:d])))
    f = reduce(lambda x, y: 0.1 * x + y, list(map(lambda x: DIGITS[x], s[:d:-1]))) * 0.1
    return i + f

  • 1

Reply