Discuss / Python / 练习题

练习题

Topic source

第一题

def normalize(name):
    return name.capitalize()

第二题

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

第三题

def str2float(s):

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


    def left_cal(x, y):
        return x * 10 + y


    def right_cal(x, y):
        return x / 10 + y


    left, right = s.split('.')
    return reduce(left_cal, map(str2num, left)) + \
           reduce(right_cal, map(str2num, right[::-1])) / 10

ar_达达达

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

from functools import reduce def str2float(s):

def str2num(s):
    return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[s]
left, right = s.split('.')
return reduce(lambda x , y: x * 10 + y, map(str2num, left)) + reduce(lambda x, y: x /10 +y, map(str2num,right[::-1])) / 10

print('str2float(\'123.456\') =', str2float('123.456'))


  • 1

Reply