Esempio n. 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)
Esempio n. 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)
Esempio n. 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()
Esempio n. 4
0
 def test_ordered_numbers(self):
     self.assertEqual(minmax([0, 1, 2, 3, 4]), (0, 4))
Esempio n. 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")
Esempio n. 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([]))
Esempio n. 7
0
 def test_very_large_numbers(self):
     self.assertEqual(minmax([2**1000, -2**1000]), (-2**1000, 2**1000))
Esempio n. 8
0
 def test_error_on_empty_iterable(self):
     with self.assertRaises(ValueError):
         minmax([])
Esempio n. 9
0
 def test_strings(self):
     words = ["alfalfa", "animal", "apple", "acoustic", "axiom"]
     self.assertEqual(minmax(words), ("acoustic", "axiom"))
Esempio n. 10
0
 def test_mixed_types(self):
     with self.assertRaises(TypeError):
         minmax(["a", 2])
Esempio n. 11
0
 def test_negative_numbers(self):
     self.assertEqual(minmax([-10, -8, -7, -5, -3]), (-10, -3))
Esempio n. 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))
Esempio n. 13
0
 def test_single_item(self):
     self.assertEqual(minmax([10]), (10, 10))
Esempio n. 14
0
 def test_with_out_of_order_numbers(self):
     self.assertEqual(minmax([10, 8, 7, 5.0, 3, 6, 2]), (2, 10))
Esempio n. 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'