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 least-common-multiple
- Loading branch information
Showing
6 changed files
with
291 additions
and
1 deletion.
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
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,56 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
""" | ||
A module to find the maximum length of the | ||
concatenation of a subsequence of arr that has unique characters. | ||
Return the maximum possible length of s | ||
Module contents: | ||
- maxLength: Function to get the Maximum Length of a Concatenated String with Unique Characters | ||
- backtrack: Helper function to perform backtracking | ||
Created on 2024-01-12 | ||
Author: Kefah Albashityalshaer | ||
""" | ||
|
||
|
||
def max_length(arr: list) -> int: | ||
""" | ||
Function to get the Maximum Length of a Concatenated String with Unique Characters | ||
Takes a list of strings and returns the max length of concatenated string with unique characters | ||
Parameters: | ||
arr: list of stings elements to process | ||
Returns -> int: the maximum length | ||
Examples: | ||
>>> max_length(["un","iq","ue"]) | ||
4 | ||
>>> max_length(["cha","r","act","ers"]) | ||
6 | ||
>>> max_length(["abcdefghijklmnopqrstuvwxyz"]) | ||
26 | ||
>>> max_length(["a", "bb", "ccc"]) | ||
1 | ||
>>> max_length([]) | ||
0 | ||
""" | ||
assert isinstance(arr, list), "input should be a list of strings" | ||
|
||
# Helper function to perform backtracking | ||
def backtrack(index, current_string): | ||
# If the current string has duplicate characters, return 0 | ||
if len(current_string) != len(set(current_string)): | ||
return 0 | ||
|
||
# Calculate the maximum length by trying to add each subsequent string | ||
max_length = len(current_string) | ||
for i in range(index, len(arr)): | ||
max_length = max(max_length, backtrack(i + 1, current_string + arr[i])) | ||
return max_length | ||
|
||
# Start the backtracking from index 0 and an empty string | ||
return backtrack(0, "") |
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,70 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Test module for maximum_length function. | ||
Test categories: | ||
- Standard cases: list of strings that may have unique or overlapping characters | ||
- Edge cases: empty lists, strings with identical bounds or duplicate characters, substrings with repetitive letters | ||
- Defensive tests: invalid inputs, assertions, or `TypeError` exceptions | ||
Created on 2024-01-12 | ||
Author: Kefah Albashityalshaer | ||
""" | ||
|
||
import unittest | ||
|
||
from ..maximum_length import max_length | ||
|
||
|
||
class TestMaxLengthFunction(unittest.TestCase): | ||
# Standard test cases | ||
def test_basic_cases(self): | ||
self.assertEqual( | ||
max_length(["un", "iq", "ue"]), 4 | ||
) # "uniq" forms a concatenated string of length 4 | ||
self.assertEqual( | ||
max_length(["cha", "r", "act", "ers"]), 6 | ||
) # The longest concatenated string is "acters" (length 6) | ||
self.assertEqual( | ||
max_length(["abcdefghijklmnopqrstuvwxyz"]), 26 | ||
) # The string contains all unique lowercase letters (length 26) | ||
|
||
# Edge Cases | ||
def test_empty_list(self): | ||
self.assertEqual( | ||
max_length([]), 0 | ||
) # No strings to concatenate, so the result is 0 | ||
|
||
def test_single_element(self): | ||
self.assertEqual(max_length(["a"]), 1) | ||
self.assertEqual(max_length(["abcdef"]), 6) | ||
|
||
def test_no_unique_concatenation(self): | ||
self.assertEqual(max_length(["a", "bb", "ccc"]), 1) | ||
|
||
def test_repeated_characters(self): | ||
self.assertEqual(max_length(["abc", "def", "abc"]), 6) | ||
|
||
def test_large_input(self): | ||
arr = ["ab", "cd", "ef", "gh", "ij", "kl", "mnop"] | ||
self.assertEqual(max_length(arr), 16) # Longest possible combination | ||
|
||
# Defensive tests | ||
def test_invalid_input(self): | ||
with self.assertRaises( | ||
AssertionError, | ||
msg="Input should raise an AssertionError for non-list input.", | ||
): | ||
max_length("not a list") | ||
|
||
def test_non_string_input(self): | ||
with self.assertRaises(TypeError): | ||
max_length(["abc", 123, "def"]) | ||
|
||
def test_empty_strings(self): | ||
self.assertEqual(max_length(["", "abc", "de"]), 5) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
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() |