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 #37 from MIT-Emerging-Talent/check-if-coprime
Check if coprime
- Loading branch information
Showing
2 changed files
with
106 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,53 @@ | ||
"""Module to check if two numbers are relatively prime. | ||
This module defines a function to determine whether two numbers (Integer) are relatively | ||
prime, meaning they have no common factors than 1. | ||
@author: Vahab | ||
@Created: 11/01/2025 | ||
""" | ||
|
||
|
||
def check_coprime(first_integer: int, second_integer: int) -> bool: | ||
"""Check if two numbers are relatively prime. | ||
Two numbers are relatively prime when there | ||
are no common factors other than 1 | ||
Parameters: | ||
first_integer (int): The first number (must be a positive integer). | ||
second_integer (int): The second number (must be a positive integer). | ||
Returns: | ||
bool: True if the numbers are relatively prime, False otherwise. | ||
Raises: | ||
ValueError: If either of the inputs is not a positive integer. | ||
TypeError: If either of the inputs is not an integer. | ||
>>> check_coprime(15, 28) | ||
True | ||
>>> check_coprime(8, 12) | ||
False | ||
>>> check_coprime(7, 9) | ||
True | ||
""" | ||
|
||
# Check if inputs are integers | ||
if not isinstance(first_integer, int) or not isinstance(second_integer, int): | ||
raise TypeError("Both inputs must be integers.") | ||
|
||
# Check if inputs are positive integers | ||
if first_integer <= 0 or second_integer <= 0: | ||
raise ValueError("Both numbers must be positive integers.") | ||
|
||
# Find the smaller of the two numbers | ||
smaller = min(first_integer, second_integer) | ||
|
||
# Check for any common factors greater than 1 | ||
for i in range(2, smaller + 1): | ||
if first_integer % i == 0 and second_integer % i == 0: | ||
return False | ||
|
||
return True |
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,53 @@ | ||
"""Tests for check_coprime function. | ||
This module contains unit tests to ensure the check_coprime function works as expected, | ||
including handling edge cases and defensive assertions. | ||
@author: Vahab | ||
@created: 11/01/2025 | ||
""" | ||
|
||
import unittest | ||
|
||
|
||
from solutions.check_coprime import check_coprime | ||
|
||
|
||
class TestCoprimeFunction(unittest.TestCase): | ||
"""Test cases for the check_coprime function.""" | ||
|
||
def test_coprime_true(self): | ||
"""Test case where the numbers are relatively prime.""" | ||
self.assertTrue(check_coprime(15, 28)) | ||
|
||
def test_coprime_false(self): | ||
"""Test case where the numbers are not relatively prime.""" | ||
self.assertFalse(check_coprime(8, 12)) | ||
|
||
def test_coprime_same_value(self): | ||
"""Test case where both numbers are the same and greater than 1.""" | ||
self.assertFalse(check_coprime(12, 12)) | ||
|
||
def test_coprime_negative_input(self): | ||
"""Test case where negative numbers are provided.""" | ||
with self.assertRaises(ValueError): | ||
check_coprime(-5, 28) | ||
|
||
def test_coprime_non_integer_input(self): | ||
"""Test case where non-integer inputs are provided.""" | ||
with self.assertRaises(TypeError): | ||
check_coprime("15", 28) | ||
|
||
def test_coprime_large_numbers(self): | ||
"""Test case with large numbers to check performance.""" | ||
self.assertTrue(check_coprime(1, 100)) | ||
|
||
def test_coprime_edge_case(self): | ||
"""Test case where one input is 0.""" | ||
with self.assertRaises(ValueError): | ||
check_coprime(0, 10) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |