Discuss / Python / 作业捏

作业捏

Topic source

浪人桃

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

Work one:

def normalize(name):
    new_name = [sb.capitalize() for sb in name]
    return new_name
L = ['adam', 'LISA', 'barT']
new_L = normalize(L)
print(new_L)

Work two:

def prod(L):
    def fn(x, y):
        return x * y
    return reduce(fn, L)
L = [1, 2, 3, 4, 5]
product = prod(L)
print(product)

Work three:

DIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
def str2float(s=[]):
    def char2num(x):
        return DIGITS[x]
    def fn(x, y):
        return x * 10 + y
    i = s.index('.')
    c1 = s[:i]
    c2 = s[i+1:]
    num1 = reduce(fn, map(char2num, c1))
    num2 = reduce(fn, map(char2num, c2))
    return num1 + num2 / 10**len(c2)
s = '123.456'
res = str2float(s)
print(res)


  • 1

Reply