# -*- coding: utf-8 -*-
# 請使用迭代查找一個list中最小和最大值,並返回一個tuple
from collections import Iterable
def findMinAndMax(L):
if len(L) == 0:
return (None,None)
if isinstance(L,Iterable) == True:
min = L[0]
max = 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('測試成功!')