Discuss / Python / 代码有点丑.

代码有点丑.

Topic source

第一题

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


L = ['adam', 'LISA', 'barT']
print(list(map(normalize, L)))

第二题

from functools import reduce


def prod(L):
    def ride(x, y):
        return x * y

    return reduce(ride, L)


print(prod([1, 2, 3, 4, 5]))

第三题

from functools import reduce


def str2float(s):
    def fn(x, y):
        return x * 10 + y

    def char2num(s):
        L = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '.': '.'}
        return L[s]

    s = list(map(char2num, s))
    pos = s.index('.')
    s.pop(pos)
    num = pow(10, len(s) - 1) / 1.0
    return reduce(fn, s) / num
num = pow(10, len(s) - 1) / 1.0

不应该减1,应该减 pos


  • 1

Reply