forked from MIT-Emerging-Talent/ET6-practice-code-review
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from MIT-Emerging-Talent/finding_maximum_minimum
finding_maximum_minimum
- Loading branch information
Showing
2 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#!/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. | ||
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 | ||
""" | ||
|
||
from typing import List, Tuple | ||
|
||
|
||
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[float]): A list of numerical values (integers or floats). | ||
Returns: | ||
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, 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#!/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 | ||
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_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() |