Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Done least common multiple challenge and its test file #65

Merged
merged 2 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions solutions/least_common_multiple.py
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
61 changes: 61 additions & 0 deletions solutions/tests/test_least_common_multiple.py
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")
Loading