Discuss / Python / 作业

作业

Topic source

EddieLau_

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

第一题

def normalize(name):
    if isinstance(name, str) == False: return None
    return name[0].upper() + name[1:].lower()

第二题

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

第三题

def str2float(s):
    # 数字字符映射数字
    def str2Num(ch) :
        return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[ch]
    
    # 找出小数点位置
    p = s.index('.')

    # Step: 1)字符转数字列表; 2)数字列表转数字(lambda方式实现); 3)除以小数位数
    return reduce(lambda x, y: x * 10 + y, map(str2Num, s.replace('.', ''))) / (10 ** (len(s) - p - 1))

  • 1

Reply