From 8db32a2b985f79614bf827cd10542615f1717cf4 Mon Sep 17 00:00:00 2001 From: Mohamed Elnageeb Date: Wed, 8 Jan 2025 10:35:04 +0200 Subject: [PATCH 1/3] fixed doctsring to only explain bahaviour rather than strategy --- solutions/sort_colors.py | 29 ++++++----------------------- 1 file changed, 6 insertions(+), 23 deletions(-) diff --git a/solutions/sort_colors.py b/solutions/sort_colors.py index 0a2db45f9..d23d7795b 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,7 @@ 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). + AssertionError: If the input is not a list or contains invalid elements (numbers out of the range 0 to 2). Examples: >>> nums = [0, 2, 1] @@ -61,18 +46,16 @@ def sort_colors(nums: List[int]) -> None: >>> nums [0, 1, 1, 2, 2] """ - # Validate input + # Input validation 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." - # Create a counter list to store the count of each number + # Count occurrences of 0, 1, and 2 counter_list = [0] * 3 - - # Count occurrences of each number 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]): From e646e0564fd6f2cc57a632d0ee9ae47058488630 Mon Sep 17 00:00:00 2001 From: Mohamed Elnageeb <128221037+Mohamed-Elnageeb@users.noreply.github.com> Date: Wed, 8 Jan 2025 12:58:39 +0300 Subject: [PATCH 2/3] Update sort_colors.py --- solutions/sort_colors.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/solutions/sort_colors.py b/solutions/sort_colors.py index d23d7795b..8e5543742 100644 --- a/solutions/sort_colors.py +++ b/solutions/sort_colors.py @@ -28,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] @@ -46,9 +47,15 @@ def sort_colors(nums: List[int]) -> None: >>> nums [0, 1, 1, 2, 2] """ - # Input validation - 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.") + + # 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 0, 1, and 2 counter_list = [0] * 3 From 832ef268f21efec9c5e284130770bee14d299332 Mon Sep 17 00:00:00 2001 From: Mohamed Elnageeb Date: Wed, 8 Jan 2025 14:16:18 +0200 Subject: [PATCH 3/3] fixed tests and function docstring --- solutions/sort_colors.py | 15 +++++++++++---- solutions/tests/test_sort_colors.py | 24 ++++++++++++++++-------- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/solutions/sort_colors.py b/solutions/sort_colors.py index d23d7795b..8e5543742 100644 --- a/solutions/sort_colors.py +++ b/solutions/sort_colors.py @@ -28,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] @@ -46,9 +47,15 @@ def sort_colors(nums: List[int]) -> None: >>> nums [0, 1, 1, 2, 2] """ - # Input validation - 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.") + + # 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 0, 1, and 2 counter_list = [0] * 3 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)