Discuss / Python / 交作业

交作业

Topic source

___Hiboboo

#1 Created at ... [Delete] [Delete and Lock User]
# -*- coding: utf-8 -*-
from collections import Iterator


def findMinAndMax(L):
    if not isinstance(L, Iterator):
        length = len(L)
        if length == 0:
            return None, None
        if length == 1:
            return L[0], L[0]
    min = L[0]
    max = L[0]
    for value in L:
        if not isinstance(value, (int, float)):
            continue
        else:
            if min > value:
                min = value
            if max < value:
                max = value

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

  • 1

Reply