From 85bb9f1ea98a11d08e49b86c8365f2ae7133a589 Mon Sep 17 00:00:00 2001 From: Vahablotfi Date: Sat, 11 Jan 2025 20:13:14 -0700 Subject: [PATCH 1/4] check_if_coprime solution added --- solutions/check_coprime.py | 53 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 solutions/check_coprime.py diff --git a/solutions/check_coprime.py b/solutions/check_coprime.py new file mode 100644 index 000000000..30e8614ae --- /dev/null +++ b/solutions/check_coprime.py @@ -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 From 6058f756255e7b60caa7c464119aa98646b91b80 Mon Sep 17 00:00:00 2001 From: Vahablotfi Date: Sat, 11 Jan 2025 20:37:00 -0700 Subject: [PATCH 2/4] unit test for check_coprime added --- solutions/tests/test_check_coprime.py | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 solutions/tests/test_check_coprime.py diff --git a/solutions/tests/test_check_coprime.py b/solutions/tests/test_check_coprime.py new file mode 100644 index 000000000..58bd1dcbe --- /dev/null +++ b/solutions/tests/test_check_coprime.py @@ -0,0 +1,9 @@ +"""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 +""" From a5bf8cbb3b24876005dc88c095aae493f2caf6a0 Mon Sep 17 00:00:00 2001 From: Vahablotfi Date: Sat, 11 Jan 2025 21:10:17 -0700 Subject: [PATCH 3/4] unit test for check_coprime completed --- solutions/tests/test_check_coprime.py | 44 +++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/solutions/tests/test_check_coprime.py b/solutions/tests/test_check_coprime.py index 58bd1dcbe..6269710db 100644 --- a/solutions/tests/test_check_coprime.py +++ b/solutions/tests/test_check_coprime.py @@ -7,3 +7,47 @@ @author: Vahab @created: 11/01/2025 """ + +import unittest + + +from 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() From c6260175b94ff0485fbb9f4602fce65b93d5d813 Mon Sep 17 00:00:00 2001 From: Vahablotfi Date: Sat, 11 Jan 2025 21:40:20 -0700 Subject: [PATCH 4/4] fix import module in unit test file --- solutions/tests/test_check_coprime.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solutions/tests/test_check_coprime.py b/solutions/tests/test_check_coprime.py index 6269710db..0cd2afef5 100644 --- a/solutions/tests/test_check_coprime.py +++ b/solutions/tests/test_check_coprime.py @@ -11,7 +11,7 @@ import unittest -from check_coprime import check_coprime +from solutions.check_coprime import check_coprime class TestCoprimeFunction(unittest.TestCase):