def findMinAndMax(L):
if L ==[]:
return (None,None)
Max=L[0]
Min=L[0]
for x in L:
if x > Max:
Max=x
if x < Min:
Min=x
return (Min,Max)
# 测试
if findMinAndMax([]) != (None, None):
print('测试失败!')
elif findMinAndMax([7]) != (7, 7):
print('测试失败!')
elif findMinAndMax([7, 1]) != (1, 7):
print('测试失败!')
elif findMinAndMax([7, 1, 3, 9, 5]) != (1, 9):
print('测试失败!')
else:
print('测试成功!')
L1 = ['Hello', 'World', 18, 'Apple', None]
L1 = [ s.lower() for s in L1 if isinstance(s,str)]
print(L1)
def triangles():
L0 = [1]
L1 = [1]
while True:
yield L1
L1=[1]
if L0 != [1]:
for i in range(1,len(L0)):
L1.append(L0[i]+L0[i-1])
L1.append(1)
L0=L1
pass
return 'done'
n = 0
results = []
for t in triangles():
print(t)
results.append(t)
n = n + 1
if n == 10:
break
if results == [
[1],
[1, 1],
[1, 2, 1],
[1, 3, 3, 1],
[1, 4, 6, 4, 1],
[1, 5, 10, 10, 5, 1],
[1, 6, 15, 20, 15, 6, 1],
[1, 7, 21, 35, 35, 21, 7, 1],
[1, 8, 28, 56, 70, 56, 28, 8, 1],
[1, 9, 36, 84, 126, 126, 84, 36, 9, 1]
]:
print('测试通过!')
else:
print('测试失败!')
from functools import reduce
def normalize(name):
ans = str(name[:1]).upper()
ans += str(name[1:]).lower()
return ans
# 测试:
L1 = ['adam', 'LISA', 'barT']
L2 = list(map(normalize, L1))
print(L2)
from functools import reduce
def mul(x,y):
return x * y
def prod(L):
return reduce(mul,L)
print('3 * 5 * 7 * 9 =', prod([3, 5, 7, 9]))
if prod([3, 5, 7, 9]) == 945:
print('测试成功!')
else:
print('测试失败!')
from functools import reduce
digit = {'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,
'6':6,'7':7,'8':8,'9':9}
def c2n(c):
return digit[c]
def integer(x,y):
return x * 10 + y
def decimal(x,y):
return x * 0.1 + y
def str2float(s):
i = str(s).index('.')
s1 = s[:i]
s2 = reversed(s[-i:])
return reduce(integer,map(c2n,s1)) + reduce(decimal,map(c2n,s2)) * 0.1
#测试
print('str2float(\'123.456\') =', str2float('123.456'))
if abs(str2float('123.456') - 123.456) < 0.00001:
print('测试成功!')
else:
print('测试失败!')
def _odd_iter():
n = 1
while True:
n = n + 2
yield n
def _not_divisible(n):
return lambda x: x % n > 0
def primes():
yield 2
it = _odd_iter() # 初始序列
while True:
n = next(it) # 返回序列的第一个数
yield n
it = filter(_not_divisible(n), it) # 构造新序列
# 打印1000以内的素数:
for n in primes():
if n < 1000:
print(n)
else:
break
def is_palindrome(n):
t = n
r = 0
while t > 0:
r = r * 10 +t%10
t//=10
return r == n
# 测试:
output = filter(is_palindrome, range(1, 1000))
print('1~1000:', list(output))
if list(filter(is_palindrome, range(1, 200))) == [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111, 121, 131, 141, 151, 161, 171, 181, 191]:
print('测试成功!')
else:
print('测试失败!')
L = [('Bob', 75), ('Adam', 92), ('Bart', 66), ('Lisa', 88)]
def by_name(t):
x,y = t
return x
def by_score(t):
x,y = t
return - y
def by_score_name(t):
return t[1],t[0]
L2 = sorted(L, key=by_name)print(L2)L2 = sorted(L, key=by_score)print(L2)
L2 = sorted(L, key=by_score_name)
print(L2)
def createCounter():
def g():
n = 0
while True:
n += 1
yield n
num = g()#只有显示调用函数才可以生成一个generator对象
def counter():
return next(num)
return counter
def createCounter():
l = [0]#列表是全局变量
def counter():
l[0]+=1
return l[0]
return counter
# 测试:
counterA = createCounter()
print(counterA(), counterA(), counterA(), counterA(), counterA()) # 1 2 3 4 5
counterB = createCounter()
if [counterB(), counterB(), counterB(), counterB()] == [1, 2, 3, 4]:
print('测试通过!')
else:
print('测试失败!')