Skip to content

Commit

Permalink
Merge pull request #86 from MIT-Emerging-Talent/23-sort-colors
Browse files Browse the repository at this point in the history
23 sort colors
  • Loading branch information
Mohamed-Elnageeb authored Jan 8, 2025
2 parents 1c7c129 + 1d0ada4 commit f3bdf87
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 33 deletions.
40 changes: 15 additions & 25 deletions solutions/sort_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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]
Expand All @@ -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]):
Expand Down
24 changes: 16 additions & 8 deletions solutions/tests/test_sort_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit f3bdf87

Please sign in to comment.