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 pull request #65 from MIT-Emerging-Talent/least-common-multiple
Done least common multiple challenge and its test file
- Loading branch information
Showing
2 changed files
with
105 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,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,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") |