python 练习题:使用迭代查找一个list中最小和最大值,并返回一个tuple

# -*- 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('测试成功!')

  

    原文作者:it_逗逗
    原文地址: https://www.cnblogs.com/chling/p/11759015.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞