Discuss / Python / 作业——by自来也

作业——by自来也

Topic source

第一题:

L1 = ['adam', 'LISA', 'barT']
def normalize(name):
    return str.capitalize(name)

L2 = list(map(normalize, L1))
print L2

第二题:

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

print('3 * 5 * 7 * 9 =', prod([3, 5, 7, 9]))

第三题:

def str2float(s):
    "str2float(s): '123.456'->['123', '456']->[123, 456]->[123.0, 0.456]->123.456"
    return list2float(map(str2int ,split(s)))

def str2int(s):
    "str2int(s): '123'->[1, 2, 3]->123"
    return reduce(num2int, map(ch2int, s))

def ch2int(c):
    "ch2int(c): '1'->1"
    return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[c]

def split(s):
    "'123.456'->['123', '456']"
    return s.split('.')

def num2int(high, low):
    "1,2 -> 12"
    return high*10 + low

def int2dec(i):
    "456 -> 0.456"
    return i/(10.0**intLen(i))

def intLen(i):
    "整数的长度"
    return len("%d"%i)

def list2float(list):
    """
    整数部分和小数部分合成一个浮点数
    [123, 456]->[123, 0.456]->123.456
    """
    return list[0] + int2dec(list[1])

  • 1

Reply