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 61-names-asc-sorter-new
- Loading branch information
Showing
5 changed files
with
271 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
""" | ||
A module for calculating the Least Common Multiple (LCM) of a list of three integers | ||
Module contents: | ||
- least_common_multiple: function to calculates the LCM | ||
Created on 12/01/2025 | ||
@author: Amin | ||
""" | ||
|
||
|
||
def least_common_multiple(nums: list) -> int: | ||
"""calculate the Least Common Multiple for a list of three integers. | ||
Parameters: | ||
nums: list, the input list containing the three integers | ||
Returns -> int: the Least Common Multiple for the three integers | ||
Raises: | ||
AssertionError: if the argument is not a list | ||
Examples: | ||
>>> least_common_multiple([1, 2, 3]) | ||
6 | ||
>>> least_common_multiple([3, 4, 5]) | ||
60 | ||
>>> least_common_multiple([2, 1, 5]) | ||
10 | ||
""" | ||
assert isinstance(nums, list), "input must be a list" | ||
# Check if the input list contains three elements | ||
if len(nums) != 3: | ||
return -1 | ||
# Sort the input list of integers | ||
nums.sort() | ||
# Iterate through multiples of the greatest number (c) within a range up to the numbers product | ||
# Return the first multiple that is divisible by the other numbers (b, a) | ||
c = nums[2] | ||
b = nums[1] | ||
a = nums[0] | ||
for i in range(c, c * b * a + 1, c): | ||
if i % b == 0 and i % a == 0: | ||
return i |
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,61 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
""" | ||
A module for testing the least_common_multiple function. | ||
Test cases include: | ||
- Valid inputs: a list of three integers. | ||
- Sorted and unsorted numbers. | ||
- Invalid inputs: a list of invalid range. | ||
- A list containing number formats other than integers. | ||
- Edge cases: special numbers such as 0 and negative numbers. | ||
Created on 12/01/2025 | ||
@author: Amin | ||
""" | ||
|
||
import unittest | ||
|
||
from ..least_common_multiple import least_common_multiple | ||
|
||
|
||
class TestLeastCommonMultiple(unittest.TestCase): | ||
"""Test the least_common_multiple function""" | ||
|
||
def test_sorted_list(self): | ||
"""It should return the Least Common Multiple for a sorted list of three integers""" | ||
self.assertEqual(least_common_multiple([2, 3, 4]), 12) | ||
|
||
def test_unsorted_list(self): | ||
"""It should return the Least Common Multiple for a sorted list of three integers""" | ||
self.assertEqual(least_common_multiple([5, 2, 4]), 20) | ||
|
||
def test_more_than_three_numbers(self): | ||
"""It should return -1 for a list containing more than three integers""" | ||
self.assertEqual(least_common_multiple([1, 2, 3, 4]), -1) | ||
|
||
def test_fewer_than_three_numbers(self): | ||
"""It should return -1 for a list containing fewer than three integers""" | ||
self.assertEqual(least_common_multiple([1, 2]), -1) | ||
|
||
def test_empty_list(self): | ||
"""It should return -1 for an empty list""" | ||
self.assertEqual(least_common_multiple([]), -1) | ||
|
||
def test_including_zero(self): | ||
"""It should return None for lists including 0""" | ||
self.assertEqual(least_common_multiple([0, 2, 4]), None) | ||
|
||
def test_including_negative_numbers(self): | ||
"""It should return None for lists including 0""" | ||
self.assertEqual(least_common_multiple([-1, 9, 5]), None) | ||
|
||
def test_non_integer_numbers(self): | ||
"""It should raise ValueError for lists containing numbers other than integers""" | ||
with self.assertRaises(TypeError): | ||
least_common_multiple([1.2, 5, 2]) | ||
|
||
def test_not_list(self): | ||
"""It should raise AssertionError for non-list inputs""" | ||
with self.assertRaises(AssertionError): | ||
least_common_multiple("not a list") |
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() |