Discuss / Python / 交作业

交作业

Topic source

老帕小帕

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

第一题

# Solution 1
def normalize(name):
    return name[0].upper + name[1:].lower()
# Solution 2
def normalize(name):
    return name.capitalize

第二题

#Solution 1
def prod(s):
    def mult(x,y):
        return x*y
    return reduce(mult,s)
#Solution 2:
def prod(s):
    return reduce(lambda x, y: x*y, s)

第三题

def str2float(s):
    above1,below1 = s.split('.')
    def char2num(s): 
        return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[s]
    a1 = reduce (lambda x,y:x*10+y,map(char2num,above1))
    b1 = (reduce (lambda x,y:x*10+y,map(char2num,below1)))/(10 ** (len((below1))))
    return a1+b1

  • 1

Reply