Discuss / Python / 挺有趣的

挺有趣的

Topic source

#Q1

def normalize(name):

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

#Q2

def prod(L):

    def pn(x,y):

        return x * y

    return reduce(pn,L)

#Q3

    def char2num(s):

DIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}

return DIGITS[s]

position = s.index('.')

a = s[:position]

b = s[position+1:]

intnum = reduce(lambda x, y: x * 10 + y , map(char2num, a))

floatnum = reduce(lambda x , y : x * 0.1 + y , list(map(char2num,b))[::-1])

newnum = intnum + 0.1 * floatnum

return newnum


  • 1

Reply