Discuss / Python / Macyrate

Macyrate

Topic source

Macyrate

#1 Created at ... [Delete] [Delete and Lock User]
# -*- coding: utf-8 -*-
def normalize(name):
    return name[0].upper()+name[1:len(name)].lower()
# 测试:
L1 = ['adam', 'LISA', 'barT']
L2 = list(map(normalize, L1))
print(L2)
# -*- coding: utf-8 -*-
def prod(L):
    def multi(x,y):
        return x*y
    return reduce(multi,L)

print('3 * 5 * 7 * 9 =', prod([3, 5, 7, 9]))
if prod([3, 5, 7, 9]) == 945:
    print('测试成功!')
else:
    print('测试失败!')
# -*- coding: utf-8 -*-    
    def str2int(s):
        DIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
        def fn(x, y):
            return x * 10 + y
        def char2num(s):
            return DIGITS[s]
        return reduce(fn, map(char2num, s))

    return str2int(s.split('.')[0]+s.split('.')[1])*(0.1**len(s.split('.')[1]))    #split这个方法还蛮有用的

print('str2float(\'123.456\') =', str2float('123.456'))
if abs(str2float('123.456') - 123.456) < 0.00001:
    print('测试成功!')
else:
    print('测试失败!')

  • 1

Reply