From 4356c482093de12902f2500c82349c7f9f002537 Mon Sep 17 00:00:00 2001 From: Chandan <141244019+chan9an@users.noreply.github.com> Date: Wed, 29 Oct 2025 22:44:10 +0530 Subject: [PATCH 1/5] Add Dutch National Flag sorting algorithm Implements the Dutch National Flag algorithm to sort an array of 0s, 1s, and 2s in-place. Includes error handling for invalid values and provides examples in the docstring. --- .../arrays/dutch_national_flag_sort.py | 108 ++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 data_structures/arrays/dutch_national_flag_sort.py diff --git a/data_structures/arrays/dutch_national_flag_sort.py b/data_structures/arrays/dutch_national_flag_sort.py new file mode 100644 index 000000000000..ff4833905d12 --- /dev/null +++ b/data_structures/arrays/dutch_national_flag_sort.py @@ -0,0 +1,108 @@ +""" +Sorts an array consisting of 0s, 1s, and 2s using the Dutch National Flag algorithm. + +This algorithm sorts the array in a single pass with O(n) time complexity and O(1) space complexity. +It uses three pointers to partition the array into sections for 0s, 1s, and 2s. + +https://en.wikipedia.org/wiki/Dutch_national_flag_problem +""" + +def dutch_flag_sort(arr: list[int]) -> None: + """ + Sorts an array of 0s, 1s, and 2s in-place. + + Args: + arr: The list to sort, containing only 0, 1, or 2. + + Returns: + None + + Raises: + ValueError: If the array contains values other than 0, 1, or 2. + + Examples: + >>> arr = [2, 0, 2, 1, 1, 0] + >>> dutch_flag_sort(arr) + >>> arr + [0, 0, 1, 1, 2, 2] + >>> arr = [2, 0, 1] + >>> dutch_flag_sort(arr) + >>> arr + [0, 1, 2] + >>> arr = [] + >>> dutch_flag_sort(arr) + >>> arr + [] + >>> arr = [1] + >>> dutch_flag_sort(arr) + >>> arr + [1] + >>> arr = [0, 0, 0] + >>> dutch_flag_sort(arr) + >>> arr + [0, 0, 0] + >>> arr = [2, 2, 2] + >>> dutch_flag_sort(arr) + >>> arr + [2, 2, 2] + >>> arr = [1, 1, 1] + >>> dutch_flag_sort(arr) + >>> arr + [1, 1, 1] + >>> arr = [0, 1, 2, 0, 1, 2] + >>> dutch_flag_sort(arr) + >>> arr + [0, 0, 1, 1, 2, 2] + >>> arr = [2, 1, 0, 2, 1, 0] + >>> dutch_flag_sort(arr) + >>> arr + [0, 0, 1, 1, 2, 2] + >>> arr = [3] + >>> dutch_flag_sort(arr) + Traceback (most recent call last): + ... + ValueError: Array contains invalid value: 3 + >>> arr = [-1, 0, 1] + >>> dutch_flag_sort(arr) + Traceback (most recent call last): + ... + ValueError: Array contains invalid value: -1 + >>> arr = [0, 1, 2, 3] + >>> dutch_flag_sort(arr) + Traceback (most recent call last): + ... + ValueError: Array contains invalid value: 3 + >>> arr = (2, 0, 1) + >>> dutch_flag_sort(arr) + Traceback (most recent call last): + ... + TypeError: 'tuple' object does not support item assignment + >>> arr = [2, 0, '1'] + >>> dutch_flag_sort(arr) + Traceback (most recent call last): + ... + ValueError: Array contains invalid value: 1 + """ + if not arr: + return + low, mid, high = 0, 0, len(arr) - 1 + while mid <= high: + if arr[mid] == 0: + arr[low], arr[mid] = arr[mid], arr[low] + low += 1 + mid += 1 + elif arr[mid] == 1: + mid += 1 + elif arr[mid] == 2: + arr[mid], arr[high] = arr[high], arr[mid] + high -= 1 + else: + raise ValueError(f"Array contains invalid value: {arr[mid]}") + +if __name__ == "__main__": + import doctest + doctest.testmod() + + + +does it require any improvements? From c3ddf29bcc06a0740b06d37c4273fabc9e3880a4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 29 Oct 2025 17:20:41 +0000 Subject: [PATCH 2/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/dutch_national_flag_sort.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/arrays/dutch_national_flag_sort.py b/data_structures/arrays/dutch_national_flag_sort.py index ff4833905d12..ef75d8ab04cc 100644 --- a/data_structures/arrays/dutch_national_flag_sort.py +++ b/data_structures/arrays/dutch_national_flag_sort.py @@ -105,4 +105,4 @@ def dutch_flag_sort(arr: list[int]) -> None: -does it require any improvements? +does it require any improvements? From 611bad6055d828c0731f10e98b40d1f31cd6d9f1 Mon Sep 17 00:00:00 2001 From: Chandan <141244019+chan9an@users.noreply.github.com> Date: Thu, 30 Oct 2025 01:10:40 +0530 Subject: [PATCH 3/5] Delete data_structures/arrays/dutch_national_flag_sort.py --- .../arrays/dutch_national_flag_sort.py | 108 ------------------ 1 file changed, 108 deletions(-) delete mode 100644 data_structures/arrays/dutch_national_flag_sort.py diff --git a/data_structures/arrays/dutch_national_flag_sort.py b/data_structures/arrays/dutch_national_flag_sort.py deleted file mode 100644 index ef75d8ab04cc..000000000000 --- a/data_structures/arrays/dutch_national_flag_sort.py +++ /dev/null @@ -1,108 +0,0 @@ -""" -Sorts an array consisting of 0s, 1s, and 2s using the Dutch National Flag algorithm. - -This algorithm sorts the array in a single pass with O(n) time complexity and O(1) space complexity. -It uses three pointers to partition the array into sections for 0s, 1s, and 2s. - -https://en.wikipedia.org/wiki/Dutch_national_flag_problem -""" - -def dutch_flag_sort(arr: list[int]) -> None: - """ - Sorts an array of 0s, 1s, and 2s in-place. - - Args: - arr: The list to sort, containing only 0, 1, or 2. - - Returns: - None - - Raises: - ValueError: If the array contains values other than 0, 1, or 2. - - Examples: - >>> arr = [2, 0, 2, 1, 1, 0] - >>> dutch_flag_sort(arr) - >>> arr - [0, 0, 1, 1, 2, 2] - >>> arr = [2, 0, 1] - >>> dutch_flag_sort(arr) - >>> arr - [0, 1, 2] - >>> arr = [] - >>> dutch_flag_sort(arr) - >>> arr - [] - >>> arr = [1] - >>> dutch_flag_sort(arr) - >>> arr - [1] - >>> arr = [0, 0, 0] - >>> dutch_flag_sort(arr) - >>> arr - [0, 0, 0] - >>> arr = [2, 2, 2] - >>> dutch_flag_sort(arr) - >>> arr - [2, 2, 2] - >>> arr = [1, 1, 1] - >>> dutch_flag_sort(arr) - >>> arr - [1, 1, 1] - >>> arr = [0, 1, 2, 0, 1, 2] - >>> dutch_flag_sort(arr) - >>> arr - [0, 0, 1, 1, 2, 2] - >>> arr = [2, 1, 0, 2, 1, 0] - >>> dutch_flag_sort(arr) - >>> arr - [0, 0, 1, 1, 2, 2] - >>> arr = [3] - >>> dutch_flag_sort(arr) - Traceback (most recent call last): - ... - ValueError: Array contains invalid value: 3 - >>> arr = [-1, 0, 1] - >>> dutch_flag_sort(arr) - Traceback (most recent call last): - ... - ValueError: Array contains invalid value: -1 - >>> arr = [0, 1, 2, 3] - >>> dutch_flag_sort(arr) - Traceback (most recent call last): - ... - ValueError: Array contains invalid value: 3 - >>> arr = (2, 0, 1) - >>> dutch_flag_sort(arr) - Traceback (most recent call last): - ... - TypeError: 'tuple' object does not support item assignment - >>> arr = [2, 0, '1'] - >>> dutch_flag_sort(arr) - Traceback (most recent call last): - ... - ValueError: Array contains invalid value: 1 - """ - if not arr: - return - low, mid, high = 0, 0, len(arr) - 1 - while mid <= high: - if arr[mid] == 0: - arr[low], arr[mid] = arr[mid], arr[low] - low += 1 - mid += 1 - elif arr[mid] == 1: - mid += 1 - elif arr[mid] == 2: - arr[mid], arr[high] = arr[high], arr[mid] - high -= 1 - else: - raise ValueError(f"Array contains invalid value: {arr[mid]}") - -if __name__ == "__main__": - import doctest - doctest.testmod() - - - -does it require any improvements? From 33ffba8a8fd7d1ab38aa77ad4068af6d06091c0b Mon Sep 17 00:00:00 2001 From: Chandan <141244019+chan9an@users.noreply.github.com> Date: Thu, 30 Oct 2025 01:11:12 +0530 Subject: [PATCH 4/5] Add Dutch National Flag sorting algorithm Implements the Dutch National Flag algorithm to sort an array of 0s, 1s, and 2s in-place. Includes error handling for invalid values and examples in the docstring. --- .../arrays/dutch_national_flag_sort.py | 107 ++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 data_structures/arrays/dutch_national_flag_sort.py diff --git a/data_structures/arrays/dutch_national_flag_sort.py b/data_structures/arrays/dutch_national_flag_sort.py new file mode 100644 index 000000000000..dffc1b837937 --- /dev/null +++ b/data_structures/arrays/dutch_national_flag_sort.py @@ -0,0 +1,107 @@ +""" +Implements the Dutch National Flag algorithm to sort an array of 0s, 1s, and 2s +in-place. Includes error handling for invalid values and examples in the docstring. + +This algorithm runs in O(n) time and O(1) space using three pointers. +See: https://en.wikipedia.org/wiki/Dutch_national_flag_problem +""" + +from __future__ import annotations + + +def dutch_flag_sort(arr: list[int]) -> None: + """ + Sorts an array of 0s, 1s, and 2s in-place. + + Args: + arr: List containing only 0, 1, or 2. + + Returns: + None + + Raises: + ValueError: If array contains values other than 0, 1, or 2. + + Examples: + >>> arr = [2, 0, 2, 1, 1, 0] + >>> dutch_flag_sort(arr) + >>> arr + [0, 0, 1, 1, 2, 2] + + >>> arr = [2, 0, 1] + >>> dutch_flag_sort(arr) + >>> arr + [0, 1, 2] + + >>> arr = [] + >>> dutch_flag_sort(arr) + >>> arr + [] + + >>> arr = [1] + >>> dutch_flag_sort(arr) + >>> arr + [1] + + >>> arr = [0, 0, 0] + >>> dutch_flag_sort(arr) + >>> arr + [0, 0, 0] + + >>> arr = [3] + >>> dutch_flag_sort(arr) + Traceback (most recent call last): + ... + ValueError: Array contains invalid value: 3 + + >>> arr = [-1, 0, 1] + >>> dutch_flag_sort(arr) + Traceback (most recent call last): + ... + ValueError: Array contains invalid value: -1 + + >>> arr = [2, 0, '1'] + >>> dutch_flag_sort(arr) + Traceback (most recent call last): + ... + ValueError: Array contains invalid value: 1 + """ + if not arr: + return + + low = mid = 0 + high = len(arr) - 1 + + while mid <= high: + value = arr[mid] + if value == 0: + arr[low], arr[mid] = arr[mid], arr[low] + low += 1 + mid += 1 + elif value == 1: + mid += 1 + elif value == 2: + arr[mid], arr[high] = arr[high], arr[mid] + high -= 1 + else: + raise ValueError(f"Array contains invalid value: {value}") + + +if __name__ == "__main__": + examples = [ + ([2, 0, 2, 1, 1, 0],), + ([2, 0, 1],), + ([],), + ([1],), + ([0, 0, 0],), + ([2, 2, 2],), + ([1, 1, 1],), + ([0, 1, 2, 0, 1, 2],), + ([2, 1, 0, 2, 1, 0],), + ] + + for test_arr in examples: + arr = test_arr.copy() + print(f"Before: {arr}") + dutch_flag_sort(arr) + print(f"After : {arr}\n") From 3e0deb73a1d6265e20004472e7e0e05f9b5a3a08 Mon Sep 17 00:00:00 2001 From: Chandan <141244019+chan9an@users.noreply.github.com> Date: Thu, 30 Oct 2025 01:19:54 +0530 Subject: [PATCH 5/5] Improve error handling and format examples Refactor error message handling and update example array format. --- .../arrays/dutch_national_flag_sort.py | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/data_structures/arrays/dutch_national_flag_sort.py b/data_structures/arrays/dutch_national_flag_sort.py index dffc1b837937..4b6d1d091316 100644 --- a/data_structures/arrays/dutch_national_flag_sort.py +++ b/data_structures/arrays/dutch_national_flag_sort.py @@ -84,20 +84,21 @@ def dutch_flag_sort(arr: list[int]) -> None: arr[mid], arr[high] = arr[high], arr[mid] high -= 1 else: - raise ValueError(f"Array contains invalid value: {value}") + msg = f"Array contains invalid value: {value}" + raise ValueError(msg) if __name__ == "__main__": examples = [ - ([2, 0, 2, 1, 1, 0],), - ([2, 0, 1],), - ([],), - ([1],), - ([0, 0, 0],), - ([2, 2, 2],), - ([1, 1, 1],), - ([0, 1, 2, 0, 1, 2],), - ([2, 1, 0, 2, 1, 0],), + [2, 0, 2, 1, 1, 0], + [2, 0, 1], + [], + [1], + [0, 0, 0], + [2, 2, 2], + [1, 1, 1], + [0, 1, 2, 0, 1, 2], + [2, 1, 0, 2, 1, 0], ] for test_arr in examples: