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.
Merge branch 'main' into 54-maximum-length-of-a-concatenated-string-w…
…ith-unique-characters
- Loading branch information
Showing
3 changed files
with
125 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
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,44 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
""" | ||
A module for obtaining min integer from a list. | ||
Module contents: | ||
- min_integer: Returns minimum integer from a list of integers | ||
Created on 2025-01-09 | ||
Author: Tamir Elwaleeed | ||
""" | ||
|
||
|
||
def min_integer(my_list: list) -> int: | ||
"""Returns the min integer in a list of integers | ||
If the list is empty, the function returns None | ||
Parameters: | ||
my_list: list, the list to find the min from | ||
Returns -> int: the min integer from the list | ||
Raises: | ||
AssertionError: if input is not a list | ||
Examples: | ||
>>> min_integer([1, 2, 3, 4, 5]) | ||
1 | ||
>>> min_integer([-1, -4, -5]) | ||
-5 | ||
>>> min_integer([]) | ||
None | ||
""" | ||
assert isinstance(my_list, list), "input must be a list" | ||
|
||
if len(my_list) == 0: | ||
return None | ||
result = my_list[0] | ||
i = 1 | ||
while i < len(my_list): | ||
if my_list[i] < result: | ||
result = my_list[i] | ||
i += 1 | ||
return result |
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,75 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Test module for min_integer function. | ||
Test categories: | ||
- Standard cases: typical lists with different lengths | ||
- Edge cases: empty lists, single elements, strings | ||
- Defensive tests: wrong input types, assertions | ||
Created on 2025-01-09 | ||
Author: Tamir Elwaleeed | ||
""" | ||
|
||
import unittest | ||
from ..min_integer import min_integer | ||
|
||
|
||
class TestMinInteger(unittest.TestCase): | ||
"""Define unittests for min_integer([..]).""" | ||
|
||
def test_ordered_list(self): | ||
"""Test an ordered list of integers.""" | ||
ordered = [1, 2, 3, 4] | ||
self.assertEqual(min_integer(ordered), 1) | ||
|
||
def test_unordered_list(self): | ||
"""Test an unordered list of integers.""" | ||
unordered = [1, 2, 4, 3] | ||
self.assertEqual(min_integer(unordered), 1) | ||
|
||
def test_min_at_beginning(self): | ||
"""Test a list with a beginning min value.""" | ||
min_at_beginning = [1, 4, 3, 2] | ||
self.assertEqual(min_integer(min_at_beginning), 1) | ||
|
||
def test_empty_list(self): | ||
"""Test an empty list.""" | ||
empty = [] | ||
self.assertEqual(min_integer(empty), None) | ||
|
||
def test_one_element_list(self): | ||
"""Test a list with a single element.""" | ||
one_element = [7] | ||
self.assertEqual(min_integer(one_element), 7) | ||
|
||
def test_floats(self): | ||
"""Test a list of floats.""" | ||
floats = [1.53, 6.33, -9.123, 15.2, 6.0] | ||
self.assertEqual(min_integer(floats), -9.123) | ||
|
||
def test_ints_and_floats(self): | ||
"""Test a list of ints and floats.""" | ||
ints_and_floats = [1.53, 15.5, -9, 15, 6] | ||
self.assertEqual(min_integer(ints_and_floats), -9) | ||
|
||
def test_string(self): | ||
"""Test a string.""" | ||
string = "Brennan" | ||
with self.assertRaises(AssertionError): | ||
min_integer(string) | ||
|
||
def test_list_of_strings(self): | ||
"""Test a list of strings.""" | ||
strings = ["Brennan", "is", "my", "name"] | ||
self.assertEqual(min_integer(strings), "Brennan") | ||
|
||
def test_empty_string(self): | ||
"""Test an empty string.""" | ||
with self.assertRaises(AssertionError): | ||
min_integer("") | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |