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 diff --git a/solutions/find_highest_alpha_position.py b/solutions/find_highest_alpha_position.py new file mode 100644 index 000000000..c03ce0617 --- /dev/null +++ b/solutions/find_highest_alpha_position.py @@ -0,0 +1,52 @@ +"""Module for finding the highest alphabetical position in a text string. + +This module provides functionality to find the alphabetical position (1-26) +of the character that appears latest in the alphabet within a given text. + +@author: Musab Kaymak +@created: 01/09/2025 +""" + + +def find_highest_alpha_position(text: str) -> int: + """Find the alphabetical position of the latest alphabet character in text. + + Parameters: + text (str): The input text to analyze. Must contain at least one letter + and only contain English letters. Numbers and special characters are ignored. + + Returns: + int: The highest alphabetical position (1-26) found in the text. + 'a' is position 1, 'z' is position 26. + + Raises: + ValueError: If the text is empty or contains no letters. + ValueError: If the text contains non-ASCII letters. + + Examples: + >>> find_highest_alpha_position("flower") + 23 + >>> find_highest_alpha_position("apple") + 16 + >>> find_highest_alpha_position("ZEBRA") + 26 + """ + if not text: + raise ValueError("Input text cannot be empty") + + if not any(c.isalpha() for c in text): + raise ValueError("Input text must contain at least one letter") + + if not all(c.isascii() for c in text): + raise ValueError("Input text must contain only English characters") + + alphabet = "abcdefghijklmnopqrstuvwxyz" + max_position = 0 + + for char in text.lower(): + if char.isalpha(): + position = alphabet.index(char.lower()) + 1 + if position > max_position: + max_position = position + + return max_position diff --git a/solutions/get_even_numbers.py b/solutions/get_even_numbers.py new file mode 100644 index 000000000..123f15b51 --- /dev/null +++ b/solutions/get_even_numbers.py @@ -0,0 +1,49 @@ +""" +Module for filtering even numbers from a list of integers. + +This module provides a function that takes a list of integers as input +and returns a list of all even numbers from the input list. + +@author: Vahab +@created: 01/11/2025 +""" + +from typing import List + + +def get_even_numbers(numbers: List[int]) -> List[int]: + """ + Filters even numbers from a list of integers. + + Parameters: + numbers (List[int]): A list of integers to filter. + + Returns: + List[int]: A list containing all even numbers from the input list. + + Raises: + TypeError: If the input is not a list or contains non-integer elements. + + Example: + >>> get_even_numbers([1, 2, 3, 4, 5, 6]) + [2, 4, 6] + >>> get_even_numbers([1, 3, 5]) + [] + >>> get_even_numbers([]) + [] + """ + # Check the input is a list + if not isinstance(numbers, list): + raise TypeError("Input must be a list.") + + # Check the list items are only integers + if not all(isinstance(n, int) for n in numbers): + raise TypeError("All elements in the list must be integers.") + + # Filters the even number from input list and store them in a list + return [n for n in numbers if n % 2 == 0] + + +if __name__ == "__main__": + example_input = [1, 2, 3, 4, 5, 6] + print(get_even_numbers(example_input)) diff --git a/solutions/get_prime_numbers.py b/solutions/get_prime_numbers.py new file mode 100644 index 000000000..cb2820dc4 --- /dev/null +++ b/solutions/get_prime_numbers.py @@ -0,0 +1,51 @@ +# prime_numbers.py + +""" +This module provides a function to find all prime numbers up to a given integer. + +Function: + - get_prime_numbers: Returns a list of prime numbers less than or equal to a given integer. +Created on: January 6, 2025 +@author: Melat Assefa +""" + +from typing import List + + +def get_prime_numbers(n: int) -> List[int]: + """ + Returns a list of all prime numbers less than or equal to the given integer. + + Parameters: + n (int): The upper limit integer to find prime numbers up to. + + Returns: + List[int]: A list of prime numbers less than or equal to n. + + Raises: + ValueError: If n is less than 2. + TypeError: If n is not an integer. + + Examples: + >>> get_prime_numbers(10) + [2, 3, 5, 7] + >>> get_prime_numbers(2) + [2] + >>> get_prime_numbers(1) + [] + """ + if not isinstance(n, int): + raise TypeError("Input must be an integer.") + if n < 2: + return [] + + primes = [] + for num in range(2, n + 1): + is_prime = True + for i in range(2, int(num**0.5) + 1): + if num % i == 0: + is_prime = False + break + if is_prime: + primes.append(num) + return primes diff --git a/solutions/tests/test_check_coprime.py b/solutions/tests/test_check_coprime.py new file mode 100644 index 000000000..0cd2afef5 --- /dev/null +++ b/solutions/tests/test_check_coprime.py @@ -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() diff --git a/solutions/tests/test_find_highest_alpha_position.py b/solutions/tests/test_find_highest_alpha_position.py new file mode 100644 index 000000000..051185ba7 --- /dev/null +++ b/solutions/tests/test_find_highest_alpha_position.py @@ -0,0 +1,60 @@ +"""Tests for the find_highest_alpha_position function. + +@author: Musab Kaymak +@created: 01/09/2025 +""" + +import unittest + +from solutions.find_highest_alpha_position import find_highest_alpha_position + + +class TestFindHighestAlphaPosition(unittest.TestCase): + """Test cases for the find_highest_alpha_position function.""" + + def test_simple_word(self): + """Test with a simple lowercase word.""" + self.assertEqual(find_highest_alpha_position("flower"), 23) + + def test_uppercase_word(self): + """Test with an uppercase word.""" + self.assertEqual(find_highest_alpha_position("ZEBRA"), 26) + + def test_mixed_case_word(self): + """Test with a mixed case word.""" + self.assertEqual(find_highest_alpha_position("PyThOn"), 25) + + def test_single_letter(self): + """Test with a single letter.""" + self.assertEqual(find_highest_alpha_position("a"), 1) + + def test_with_spaces(self): + """Test with text containing spaces.""" + self.assertEqual(find_highest_alpha_position("hello world"), 23) + + def test_with_numbers(self): + """Test with text containing numbers.""" + self.assertEqual(find_highest_alpha_position("hello123"), 15) + + def test_with_special_characters(self): + """Test with text containing special characters.""" + self.assertEqual(find_highest_alpha_position("hello!@#"), 15) + + def test_empty_string(self): + """Test that empty string raises ValueError.""" + with self.assertRaises(ValueError, msg="Input text cannot be empty"): + find_highest_alpha_position("") + + def test_no_letters(self): + """Test that string with no letters raises ValueError.""" + with self.assertRaises( + ValueError, msg="Input text must contain at least one letter." + ): + find_highest_alpha_position("123") + + def test_non_english_characters(self): + """Test that non-English characters raise ValueError.""" + with self.assertRaises( + ValueError, msg="Input text must contain only English characters." + ): + find_highest_alpha_position("héllo") diff --git a/solutions/tests/test_get_even_numbers.py b/solutions/tests/test_get_even_numbers.py new file mode 100644 index 000000000..a2ffde8fd --- /dev/null +++ b/solutions/tests/test_get_even_numbers.py @@ -0,0 +1,64 @@ +""" +Unit tests for get_even_numbers function. + +@created: 01/11/2025 +@author: Vahab +""" + +import unittest + +from solutions.get_even_numbers import get_even_numbers + + +class TestGetEvenNumbers(unittest.TestCase): + """ + Tests for the get_even_numbers function. + """ + + def test_basic_functionality(self): + """ + Test: Check if the function returns the correct even numbers from a list. + """ + self.assertEqual(get_even_numbers([1, 2, 3, 4, 5, 6]), [2, 4, 6]) + + def test_no_even_numbers(self): + """ + Test: Check if the function returns an empty list when no even numbers are present. + """ + self.assertEqual(get_even_numbers([1, 3, 5]), []) + + def test_empty_list(self): + """ + Test: Check if the function handles an empty list correctly. + """ + self.assertEqual(get_even_numbers([]), []) + + def test_negative_numbers(self): + """ + Test: Check if the function correctly identifies even negative numbers. + """ + self.assertEqual(get_even_numbers([-1, -2, -3, -4]), [-2, -4]) + + def test_large_numbers(self): + """ + Test: Check if the function handles large numbers correctly. + """ + self.assertEqual(get_even_numbers([10**6, 10**9 + 1]), [10**6]) + + def test_invalid_input_not_list(self): + """ + Test: Check if the function raises a TypeError for non-list inputs. + """ + with self.assertRaises(TypeError): + get_even_numbers("not a list") + + def test_invalid_input_non_integer_elements(self): + """ + Test: Check if the function raises a TypeError for lists with non-integer elements. + """ + with self.assertRaises(TypeError): + get_even_numbers([1, 2, "three", 4]) + + +if __name__ == "__main__": + unittest.main() diff --git a/solutions/tests/test_get_prime_numbers.py b/solutions/tests/test_get_prime_numbers.py new file mode 100644 index 000000000..d4f67a400 --- /dev/null +++ b/solutions/tests/test_get_prime_numbers.py @@ -0,0 +1,59 @@ +# tests/test_prime_numbers.py + +""" +Unit tests for the get_prime_numbers function. +This test suite includes: +- Regular cases: Typical inputs that the function is expected to handle. +- Edge cases: Inputs that are at the boundary of what the function should handle. +- Error cases: Inputs that should raise exception due to invalid input. + +Created on: January 6, 2025 +@author: Melat Assefa +""" + +import unittest +from solutions.get_prime_numbers import get_prime_numbers + + +class TestGetPrimeNumbers(unittest.TestCase): + """Test cases for the get_prime_numbers function.""" + + # Regular Cases + def test_primes_up_to_10(self): + """Regular case: Test that the function returns correct primes up to 10.""" + self.assertEqual(get_prime_numbers(10), [2, 3, 5, 7]) + + def test_primes_up_to_2(self): + """Regular case: Test that the function returns correct primes up to 2.""" + self.assertEqual(get_prime_numbers(2), [2]) + + # Edge Cases + def test_no_primes_below_2_for_1(self): + """Edge case: Test that the function returns an empty list for n = 1.""" + self.assertEqual(get_prime_numbers(1), []) + + def test_no_primes_below_2_for_0(self): + """Edge case: Test that the function returns an empty list for n = 0.""" + self.assertEqual(get_prime_numbers(0), []) + + def test_no_primes_below_2_for_negative(self): + """Edge case: Test that the function returns an empty list for negative n.""" + self.assertEqual(get_prime_numbers(-5), []) + + def test_large_number(self): + """Edge case: Test the function with a larger number.""" + self.assertEqual(get_prime_numbers(20), [2, 3, 5, 7, 11, 13, 17, 19]) + + # Error Cases + def test_non_integer_input_raises_type_error(self): + """Error case: Test that the function raises a TypeError for non-integer input.""" + with self.assertRaises(TypeError): + get_prime_numbers("ten") + with self.assertRaises(TypeError): + get_prime_numbers(5.5) + with self.assertRaises(TypeError): + get_prime_numbers(None) + + +if __name__ == "__main__": + unittest.main()