From 4ad236acd0a7ceb745925927a8c859d346683af3 Mon Sep 17 00:00:00 2001 From: yonatanbest Date: Mon, 30 Dec 2024 13:31:03 +0300 Subject: [PATCH 1/2] finding_maximum_minimum --- solutions/finding_maximum_minimum.py | 27 +++++++++++ .../tests/test_finding_maximum_minimum.py | 46 +++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 solutions/finding_maximum_minimum.py create mode 100644 solutions/tests/test_finding_maximum_minimum.py diff --git a/solutions/finding_maximum_minimum.py b/solutions/finding_maximum_minimum.py new file mode 100644 index 000000000..9442999ca --- /dev/null +++ b/solutions/finding_maximum_minimum.py @@ -0,0 +1,27 @@ +""" +This module contains the implementation of the `find_maximum_minimum` function. +The `find_maximum_minimum` function finds the maximum and minimum values from a list of numbers. + +The function returns a tuple where the first element is the maximum value +and the second element is the minimum value from the list of numbers. + +Edge cases such as an empty list or invalid inputs are handled appropriately. + +@author: Yonatan Y. Yifat +""" + + +def find_maximum_minimum(numbers): + """ + Function to find the maximum and minimum from a list of numbers. + + Args: + numbers (list): A list of numerical values (integers or floats). + + Returns: + tuple: A tuple containing the minimum and maximum values in the list. + """ + if not numbers: + raise ValueError("The list is empty") + + return min(numbers), max(numbers) diff --git a/solutions/tests/test_finding_maximum_minimum.py b/solutions/tests/test_finding_maximum_minimum.py new file mode 100644 index 000000000..907468bff --- /dev/null +++ b/solutions/tests/test_finding_maximum_minimum.py @@ -0,0 +1,46 @@ +""" +This module contains unit tests for the `find_maximum_minimum` function. +The `find_maximum_minimum` function is tested for various scenarios including +standard cases, edge cases such as an empty list, and invalid inputs. + +These tests help ensure that the function behaves correctly and handles edge cases. + +@author: Yonatan Y. Yifat +""" + +import unittest + +from solutions.finding_maximum_minimum import find_maximum_minimum + + +class TestFindingMaximumMinimum(unittest.TestCase): + """Test suite for the find_maximum_minimum function.""" + + def test_positive_numbers(self): + """Test with a list of positive numbers.""" + numbers = [5, 10, 15, 20] + self.assertEqual(find_maximum_minimum(numbers), (5, 20)) + + def test_negative_numbers(self): + """Test with a list of negative numbers.""" + numbers = [-5, -10, -15, -20] + self.assertEqual(find_maximum_minimum(numbers), (-20, -5)) + + def test_mixed_numbers(self): + """Test with a list of mixed positive and negative numbers.""" + numbers = [-5, 10, 15, -20] + self.assertEqual(find_maximum_minimum(numbers), (-20, 15)) + + def test_single_number(self): + """Test with a list containing a single number.""" + numbers = [7] + self.assertEqual(find_maximum_minimum(numbers), (7, 7)) + + def test_empty_list(self): + """Test with an empty list, should raise ValueError.""" + with self.assertRaises(ValueError): + find_maximum_minimum([]) + + +if __name__ == "__main__": + unittest.main() From 21655a94cc4634f46700e4e350e4bbbc57f6fec2 Mon Sep 17 00:00:00 2001 From: yonatanbest Date: Fri, 3 Jan 2025 13:30:56 +0300 Subject: [PATCH 2/2] Fixed testcases and examples --- solutions/finding_maximum_minimum.py | 29 +++++++++++++++---- .../tests/test_finding_maximum_minimum.py | 28 ++++++++++++++++++ 2 files changed, 52 insertions(+), 5 deletions(-) diff --git a/solutions/finding_maximum_minimum.py b/solutions/finding_maximum_minimum.py index 9442999ca..3d0673bfc 100644 --- a/solutions/finding_maximum_minimum.py +++ b/solutions/finding_maximum_minimum.py @@ -1,3 +1,6 @@ +#!/usr/bin/env python3 +# -- coding: utf-8 -- + """ This module contains the implementation of the `find_maximum_minimum` function. The `find_maximum_minimum` function finds the maximum and minimum values from a list of numbers. @@ -10,18 +13,34 @@ @author: Yonatan Y. Yifat """ +from typing import List, Tuple + -def find_maximum_minimum(numbers): +def find_maximum_minimum(numbers: List[float]) -> Tuple[float, float]: """ Function to find the maximum and minimum from a list of numbers. Args: - numbers (list): A list of numerical values (integers or floats). + numbers (List[float]): A list of numerical values (integers or floats). Returns: - tuple: A tuple containing the minimum and maximum values in the list. + Tuple[float, float]: A tuple containing the minimum and maximum values in the list. + + Raises: + ValueError: If the list is empty or if the list contains non-numeric elements. + + Examples: + >>> find_maximum_minimum([5, 10, 15, 20]) + (5, 20) + >>> find_maximum_minimum([-5, -10, -15, -20]) + (-20, -5) + >>> find_maximum_minimum([1.5, 2.5, 3.5, 4.5]) + (1.5, 4.5) """ if not numbers: - raise ValueError("The list is empty") - + raise ValueError("The list is empty, cannot determine maximum and minimum.") + if not all(isinstance(x, (int, float)) for x in numbers): + raise ValueError( + "The list must contain only numeric values (integers or floats)." + ) return min(numbers), max(numbers) diff --git a/solutions/tests/test_finding_maximum_minimum.py b/solutions/tests/test_finding_maximum_minimum.py index 907468bff..0d698c20e 100644 --- a/solutions/tests/test_finding_maximum_minimum.py +++ b/solutions/tests/test_finding_maximum_minimum.py @@ -1,3 +1,6 @@ +#!/usr/bin/env python3 +# -- coding: utf-8 -- + """ This module contains unit tests for the `find_maximum_minimum` function. The `find_maximum_minimum` function is tested for various scenarios including @@ -36,11 +39,36 @@ def test_single_number(self): numbers = [7] self.assertEqual(find_maximum_minimum(numbers), (7, 7)) + def test_identical_numbers(self): + """Test with a list of identical numbers.""" + numbers = [10, 10, 10] + self.assertEqual(find_maximum_minimum(numbers), (10, 10)) + + def test_floats(self): + """Test with a list containing float numbers.""" + numbers = [1.5, 2.5, 3.5, 4.5] + self.assertEqual(find_maximum_minimum(numbers), (1.5, 4.5)) + def test_empty_list(self): """Test with an empty list, should raise ValueError.""" with self.assertRaises(ValueError): find_maximum_minimum([]) + def test_non_numeric_elements(self): + """Test with a list containing non-numeric elements, should raise ValueError.""" + with self.assertRaises(ValueError): + find_maximum_minimum([1, "a", 3]) + + def test_invalid_input_type(self): + """Test with a non-list input, should raise ValueError.""" + with self.assertRaises(ValueError): + find_maximum_minimum(None) + + def test_strings_in_list(self): + """Test with a list of strings, should raise ValueError.""" + with self.assertRaises(ValueError): + find_maximum_minimum(["a", "b", "c"]) + if __name__ == "__main__": unittest.main()