Beispiel #1
0
    def test_key_functions(self):
        words = ["alfalfa", "animal", "apple", "acoustic"]
        self.assertEqual(minmax(words, key=len), ("apple", "acoustic"))

        def a_count(word):
            return word.count("a")

        self.assertEqual(minmax(words, key=a_count), ("apple", "alfalfa"))
        with self.assertRaises(TypeError):
            minmax([1], lambda x: x)
Beispiel #2
0
 def test_default_value(self):
     self.assertEqual(minmax([], default=0), (0, 0))
     self.assertEqual(minmax([], default=None), (None, None))
     self.assertEqual(minmax([], default="a"), ("a", "a"))
     self.assertEqual(minmax([-10], default="a"), (-10, -10))
     self.assertEqual(minmax([10], default="a"), (10, 10))
     with self.assertRaises(TypeError):
         minmax([1], 0)
Beispiel #3
0
 def test_any_number_of_arguments(self):
     self.assertEqual(minmax(1, 8, 3, 9, 2), (1, 9))
     self.assertEqual(minmax("hi", "hey"), ("hey", "hi"))
     self.assertEqual(minmax("a"), ("a", "a"))
     self.assertEqual(minmax("abcd"), ("a", "d"))
     with self.assertRaises(TypeError):
         minmax()
Beispiel #4
0
 def test_ordered_numbers(self):
     self.assertEqual(minmax([0, 1, 2, 3, 4]), (0, 4))
Beispiel #5
0
 def test_response_min_and_max_attributes(self):
     words = ["alfalfa", "animal", "apple", "acoustic", "axiom"]
     output = minmax(words)
     self.assertEqual(output.min, "acoustic")
     self.assertEqual(output.max, "axiom")
Beispiel #6
0
 def test_with_non_lists(self):
     self.assertEqual(minmax((89, 17, 70, 9)), (9, 89))
     self.assertEqual(minmax({8, 7, 5, 3, 9, 6, 2}), (2, 9))
     self.assertEqual(minmax(n**2 for n in range(1, 4)), (1, 9))
     with self.assertRaises(ValueError):
         minmax(iter([]))
Beispiel #7
0
 def test_very_large_numbers(self):
     self.assertEqual(minmax([2**1000, -2**1000]), (-2**1000, 2**1000))
Beispiel #8
0
 def test_error_on_empty_iterable(self):
     with self.assertRaises(ValueError):
         minmax([])
Beispiel #9
0
 def test_strings(self):
     words = ["alfalfa", "animal", "apple", "acoustic", "axiom"]
     self.assertEqual(minmax(words), ("acoustic", "axiom"))
Beispiel #10
0
 def test_mixed_types(self):
     with self.assertRaises(TypeError):
         minmax(["a", 2])
Beispiel #11
0
 def test_negative_numbers(self):
     self.assertEqual(minmax([-10, -8, -7, -5, -3]), (-10, -3))
Beispiel #12
0
 def test_same_item_multiple_times(self):
     self.assertEqual(minmax([8, 8, 8]), (8, 8))
     self.assertEqual(minmax([7, 5, 6, 5, 7]), (5, 7))
Beispiel #13
0
 def test_single_item(self):
     self.assertEqual(minmax([10]), (10, 10))
Beispiel #14
0
 def test_with_out_of_order_numbers(self):
     self.assertEqual(minmax([10, 8, 7, 5.0, 3, 6, 2]), (2, 10))
Beispiel #15
0
def minmax(test, *args):
    res = args[0]
    for arg in args[1:]:
        if test(arg, res):
            res = arg
    return res


def lessthan(x, y):
    return x < y


def grtrthan(x, y):
    return x > y


if __name__ == '__main__':
    print(minmax(lessthan, 4, 2, 1, 5, 6, 3))  # 셀프 테스트 코드
    print(minmax(grtrthan, 4, 2, 1, 5, 6, 3))

# python minmax2.py
# I am: __main__
# 1
# 6

# python
import minmax2
# I am: minmax2
minmax2.minmax(minmax2.lessthan, 's', 'p', 'a', 'a')
# 'a'