forked from MIT-Emerging-Talent/ET6-practice-code-review
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
115 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,58 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
""" | ||
A module for finding the maximum number of a list of numbers. | ||
@author: Tagwa Hashim | ||
""" | ||
|
||
|
||
def max_in(numbers): | ||
""" | ||
Finds the maximum number in a list of numbers | ||
Args: | ||
numbers: A list of numbers (integers or floats). | ||
Returns: | ||
The maximum number in the list. | ||
Raises: | ||
ValueError: If the input is not a list. | ||
ValueError: If the input list is empty. | ||
TypeError: If the list contains non-numeric elements. | ||
Examples: | ||
>>> max_in([1, 5, 3, 9, 2]) | ||
9 | ||
>>> max_in([2.5, 1.02, 5.7]) | ||
5.7 | ||
>>> max_in([10.5, -5.2, 8]) | ||
10.5 | ||
>>> max_in([1, -5, -3]) | ||
1 | ||
>>> max_in([]) | ||
Traceback (most recent call last): | ||
ValueError: List cannot be empty. | ||
>>> max_in("not a list") | ||
Traceback (most recent call last): | ||
ValueError: Input must be a list. | ||
>>> max_in([1, 2, "three"]) | ||
Traceback (most recent call last): | ||
TypeError: List contains non-numeric elements. | ||
""" | ||
if not isinstance(numbers, list): | ||
raise ValueError("Input must be a list.") | ||
if not numbers: | ||
raise ValueError("List cannot be empty.") | ||
for num in numbers: | ||
if not isinstance(num, (int, float)): # Check if any element is not numeric | ||
raise TypeError("List contains non-numeric elements.") | ||
|
||
max_num = numbers[0] # Initialize max_num with the first element | ||
|
||
for num in numbers[1:]: # Iterate through the list starting from the second element | ||
if num > max_num: | ||
max_num = num | ||
|
||
return max_num |
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,57 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Module: test_max_in | ||
Description: | ||
This module contains unit tests for the 'max_in' function defined | ||
in the 'max_in.py' module. | ||
Author: Tagwa Hashim | ||
""" | ||
|
||
import unittest | ||
from ..max_in import max_in | ||
|
||
|
||
class TestMaxIn(unittest.TestCase): | ||
"""Test the max_in function""" | ||
|
||
def test_positive_integers(self): | ||
"""it should return 9 as the maximum number in this list""" | ||
|
||
self.assertEqual(max_in([1, 5, 3, 9, 2]), 9) | ||
|
||
def test_positive_floats(self): | ||
"""it should return 10.5 as the maximum number in this list""" | ||
self.assertEqual(max_in([10.5, 5.2, 8.7]), 10.5) | ||
|
||
def test_mix_types(self): | ||
"""it should return 1 as the maximum number in this list""" | ||
self.assertEqual(max_in([1, -5.5, -3]), 1) | ||
|
||
def test_empty_list(self): | ||
"""It should raise AssertionError for empty list.""" | ||
with self.assertRaises(ValueError): | ||
self.assertEqual(max_in([]), "List cannot be empty.") | ||
|
||
def test_single_element_list(self): | ||
"""It should return the single element.""" | ||
self.assertEqual(max_in([5]), 5) | ||
|
||
def test_non_list_input(self): | ||
"""It should raise AssertionError for non list input.""" | ||
with self.assertRaises(ValueError): | ||
self.assertEqual(max_in("not a list"), "Input must be a list.") | ||
|
||
def test_non_numeric_list(self): | ||
"""It should raise AssertionError for non-numeric list elements.""" | ||
with self.assertRaises(TypeError): | ||
self.assertEqual( | ||
max_in([1, 2, "three"]), "List contains non-numeric elements." | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |