forked from MIT-Emerging-Talent/ET6-practice-code-review
-
Notifications
You must be signed in to change notification settings - Fork 4
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 #119 from MIT-Emerging-Talent/find_largest
solution #3: find the largest in a list
- Loading branch information
Showing
2 changed files
with
88 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,34 @@ | ||
""" | ||
Finds the Largest Number in a List | ||
This file contains a function to find the largest number in a given list of numbers. | ||
Created on 01/10/2025 | ||
Author: Khadija Al Ramlawi | ||
""" | ||
|
||
|
||
def find_largest(numbers: list) -> int: | ||
""" | ||
Finds the largest number in the given list. | ||
Parameters: | ||
numbers (list): A list of numeric values. | ||
Returns: | ||
int: The largest number in the list. | ||
Raises: | ||
AssertionError: If the input is not a list. | ||
AssertionError: If the input list is empty. | ||
AssertionError: If the input contains non-numeric values. | ||
Examples: | ||
>>> find_largest([1, 2, 3]) | ||
3 | ||
>>> find_largest([10, 20, 30, 40]) | ||
40 | ||
>>> find_largest([-5, -1, -10]) | ||
-1 | ||
""" | ||
assert isinstance(numbers, list), "Input must be a list." | ||
assert len(numbers) > 0, "List cannot be empty." | ||
assert all(isinstance(num, (int, float)) for num in numbers), ( | ||
"All elements must be numeric." | ||
) | ||
|
||
return 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,54 @@ | ||
""" | ||
Unit test for Find Largest Number Function | ||
Created on 01/10/2025 | ||
Author : Khadija Al Ramlawi | ||
""" | ||
|
||
import unittest | ||
from solutions.find_largest import find_largest | ||
|
||
|
||
class TestFindLargest(unittest.TestCase): | ||
"""Test suite for for find_largest function.""" | ||
|
||
def test_positive_numbers(self): | ||
"""List with positive numbers""" | ||
self.assertEqual(find_largest([1, 2, 3]), 3) | ||
|
||
def test_mixed_numbers(self): | ||
"""List with both positive and negative numbers""" | ||
self.assertEqual(find_largest([-10, 0, 5, 20]), 20) | ||
|
||
def test_all_negative_numbers(self): | ||
"""List with all negative numbers""" | ||
self.assertEqual(find_largest([-5, -1, -10]), -1) | ||
|
||
def test_single_element(self): | ||
"""List with a single element""" | ||
self.assertEqual(find_largest([42]), 42) | ||
|
||
def test_large_numbers(self): | ||
"""List with large numbers""" | ||
self.assertEqual(find_largest([1000000, 5000000, 2000000]), 5000000) | ||
|
||
def test_empty_list(self): | ||
"""Empty list should raise an AssertionError""" | ||
with self.assertRaises(AssertionError): | ||
find_largest([]) | ||
|
||
def test_non_numeric_values(self): | ||
"""List with non-numeric values should raise an AssertionError""" | ||
with self.assertRaises(AssertionError): | ||
find_largest([1, 2, "three"]) | ||
|
||
def test_invalid_input_non_list(self): | ||
"""Non-list input should raise an AssertionError""" | ||
with self.assertRaises(AssertionError): | ||
find_largest(123) | ||
|
||
def test_invalid_input_none(self): | ||
"""None as input should raise an AssertionError""" | ||
with self.assertRaises(AssertionError): | ||
find_largest(None) |