Discuss / Python / 浪费时间

浪费时间

Topic source

第一题:

def normalize(name):  # normalize方法只需要将不规范的英文名字转换成规范名字即可

    return name.capitalize() # 注意区别lower()   upper()  capitalize()  titile()

第二题:

from functools import reduce

def prod(L):

    def acc(x, y):

        return x * y

    return reduce(acc,L)

第三题:

from functools import reduce

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

def str2float(s):

    L = s.split('.')

    def fn(x, y):

        return x * 10 + y

    def char2num(s):

        return DIGITS[s]

    return reduce(fn ,map(char2num ,L[0])) + reduce(fn, map(char2num, L[1]))/(pow(10, len(L[1])))


  • 1

Reply