diff --git a/solutions/sort_colors.py b/solutions/sort_colors.py index 0a2db45f9..8e5543742 100644 --- a/solutions/sort_colors.py +++ b/solutions/sort_colors.py @@ -4,37 +4,22 @@ A module for sorting a list of numbers based on their colors. Module contents: - - sort_colors: sorts a list of integers representing colors (0, 1, 2) in ascending order. + - sort_colors: Sorts a list of integers representing colors (0, 1, 2) in ascending order. Dependencies: - typing: Used for type annotations (List). -Notes: - - Does not use built-in sorting functions or libraries such as `sorted()` or `sort()`. - - Implements an in-place counting sort approach. - - Time complexity: O(n) - - Space complexity: O(1) (in-place). - Created on 26 12 2024 @author: Mohamed-Elnageeb """ -from typing import List # Importing List for type annotations +from typing import List def sort_colors(nums: List[int]) -> None: """ Sorts a given list of integers representing colors (0, 1, 2) in-place. - This function implements sorting logic manually using a counting sort approach and does not use - any built-in sorting functions or libraries, such as `sorted()` or `sort()`. - - Time Complexity: - O(n): Iterates through the list once to count occurrences and again to modify it in-place. - - Space Complexity: - O(1): Uses a fixed-size counter list of size 3. No additional space is used. - Parameters: nums: List[int] A list of integers where each integer is 0, 1, or 2. Modified in-place. @@ -43,7 +28,8 @@ def sort_colors(nums: List[int]) -> None: None Raises: - AssertionError: if the input is not a list or contains invalid elements (numbers out of the range 0 to 2). + TypeError: If the input is not a list or if elements in the list are not integers. + AssertionError: If elements in the list are outside the range 0 to 2. Examples: >>> nums = [0, 2, 1] @@ -61,18 +47,22 @@ def sort_colors(nums: List[int]) -> None: >>> nums [0, 1, 1, 2, 2] """ - # Validate input - assert isinstance(nums, list), "Input must be a list." - assert all(0 <= number <= 2 for number in nums), "All elements must be 0, 1, or 2." + # Validate input type + if not isinstance(nums, list): + raise TypeError("Input must be a list.") + if not all(isinstance(number, int) for number in nums): + raise TypeError("All elements in the list must be integers.") - # Create a counter list to store the count of each number - counter_list = [0] * 3 + # Validate input range + if not all(0 <= number <= 2 for number in nums): + raise AssertionError("All elements must be integers in the range 0, 1, or 2.") - # Count occurrences of each number + # Count occurrences of 0, 1, and 2 + counter_list = [0] * 3 for number in nums: counter_list[number] += 1 - # Rebuild the nums list in-place + # Rebuild the list based on counts index = 0 for number in range(3): for _ in range(counter_list[number]): diff --git a/solutions/tests/test_sort_colors.py b/solutions/tests/test_sort_colors.py index aa7af4392..282d5ad8e 100644 --- a/solutions/tests/test_sort_colors.py +++ b/solutions/tests/test_sort_colors.py @@ -21,9 +21,11 @@ - test_randomly_generated_list: Tests with a randomly generated list. - Exception test cases: - - test_not_a_list: Ensures an AssertionError is raised for non-list inputs. - - test_invalid_elements: Ensures an AssertionError is raised for invalid - elements (e.g., elements not in the range [0, 2]). + - test_not_a_list: Ensures a TypeError is raised for non-list inputs. + - test_invalid_elements_type: Ensures a TypeError is raised for invalid + element types (e.g., non-integer elements). + - test_invalid_elements_range: Ensures an AssertionError is raised for invalid + elements (e.g., integers not in the range [0, 2]). Created on 26 10 2024 @@ -77,13 +79,19 @@ def test_large_list(self): self.assertEqual(nums, expected) def test_not_a_list(self): - """It should raise an assertion error if the input is not a list""" - with self.assertRaises(AssertionError): + """It should raise a TypeError if the input is not a list""" + with self.assertRaises(TypeError): sort_colors("not a list") - def test_invalid_elements(self): - """It should raise an assertion error for invalid elements""" - nums = [0, 1, 3] # Invalid element 3 + def test_invalid_elements_type(self): + """It should raise a TypeError for invalid element types""" + nums = [0, 1, "2"] # Invalid element "2" (string) + with self.assertRaises(TypeError): + sort_colors(nums) + + def test_invalid_elements_range(self): + """It should raise an AssertionError for elements out of range""" + nums = [0, 1, 3] # Invalid element 3 (out of range) with self.assertRaises(AssertionError): sort_colors(nums)