From ca789b4bc48cf9a9fc9393b6c73bfc153c58cfd8 Mon Sep 17 00:00:00 2001 From: Kiran Hipparagi <49370990+KiranHipparagi@users.noreply.github.com> Date: Thu, 19 Aug 2021 21:21:39 +0530 Subject: [PATCH 01/12] Added Dutch national flag sort Algorithm --- sorts/dutch_national_flag_sort.py | 58 +++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 sorts/dutch_national_flag_sort.py diff --git a/sorts/dutch_national_flag_sort.py b/sorts/dutch_national_flag_sort.py new file mode 100644 index 000000000000..0bb30192e9db --- /dev/null +++ b/sorts/dutch_national_flag_sort.py @@ -0,0 +1,58 @@ +""" +Pure implementation of Dutch national flag problem(dnf) sort algorithm in Python +Dutch National Flag algorithm is a popular algorithm originally designed by Edsger Dijkstra. +It is the most optimal sort for 3 unique values (eg. 0,1,2) in an Array. +dnf sort can sort an array of n size with [0<=a[i]<=2] at guaranteed O(n) complexity in a single pass. + +More info on: https://en.wikipedia.org/wiki/Dutch_national_flag_problem + +For doctests run following command: +python -m doctest -v dnf_sort.py +or +python3 -m doctest -v dnf_sort.py + +For manual testing run: +python dnf_sort.py + +""" + + +# Python program to sort an array with +# 0, 1 and 2 in a single pass + +# Function to sort array +def dnf_sort( a, arr_size): + """ + Pure implementation of dnf sort algorithm in Python + :param data: 3 unique values (eg. 0,1,2) in an Array + :return: the same collection in ascending order + Examples: + Input: dnf_sort([2, 1, 0, 0,1, 2]) + Output: [0, 0, 1, 1, 2, 2] + Input: dnf_sort([0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1]) + Output: [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2] + """ + low = 0 + high = arr_size - 1 + mid = 0 + while mid <= high: + if a[mid] == 0: + a[low], a[mid] = a[mid], a[low] + low = low + 1 + mid = mid + 1 + elif a[mid] == 1: + mid = mid + 1 + else: + a[mid], a[high] = a[high], a[mid] + high = high - 1 + return a + +if __name__ == "__main__": + from doctest import testmod + + testmod() + + + user_input = input("Enter numbers separated by a comma:\n").strip() + unsorted = [int(item) for item in user_input.split(",")] +print(dnf_sort(unsorted,len(unsorted))) From 1a129a77c64618f143eace6f81c3515cd47b83f8 Mon Sep 17 00:00:00 2001 From: Kiran Hipparagi <49370990+KiranHipparagi@users.noreply.github.com> Date: Thu, 19 Aug 2021 21:31:10 +0530 Subject: [PATCH 02/12] Changed file name to dnf_sort.py --- sorts/{dutch_national_flag_sort.py => dnf_sort.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename sorts/{dutch_national_flag_sort.py => dnf_sort.py} (100%) diff --git a/sorts/dutch_national_flag_sort.py b/sorts/dnf_sort.py similarity index 100% rename from sorts/dutch_national_flag_sort.py rename to sorts/dnf_sort.py From 3cd9ba12636cb0e941bcaffe1e6adf56cecca6dd Mon Sep 17 00:00:00 2001 From: Kiran Hipparagi <49370990+KiranHipparagi@users.noreply.github.com> Date: Fri, 20 Aug 2021 09:51:16 +0530 Subject: [PATCH 03/12] Added descriptive name and type hint Added descriptive name and type hint for parameter with doctest for the function dnf_sort. --- sorts/dnf_sort.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/sorts/dnf_sort.py b/sorts/dnf_sort.py index 0bb30192e9db..98395d05451a 100644 --- a/sorts/dnf_sort.py +++ b/sorts/dnf_sort.py @@ -21,38 +21,39 @@ # 0, 1 and 2 in a single pass # Function to sort array -def dnf_sort( a, arr_size): +def dnf_sort( sequence:list, arr_size:int)->list: """ Pure implementation of dnf sort algorithm in Python :param data: 3 unique values (eg. 0,1,2) in an Array :return: the same collection in ascending order Examples: - Input: dnf_sort([2, 1, 0, 0,1, 2]) + Input: dnf_sort([2, 1, 0, 0,1, 2],6) Output: [0, 0, 1, 1, 2, 2] - Input: dnf_sort([0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1]) + Input: dnf_sort([0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1],12) Output: [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2] """ low = 0 high = arr_size - 1 mid = 0 while mid <= high: - if a[mid] == 0: - a[low], a[mid] = a[mid], a[low] + if sequence[mid] == 0: + sequence[low], sequence[mid] = sequence[mid], sequence[low] low = low + 1 mid = mid + 1 - elif a[mid] == 1: + elif sequence[mid] == 1: mid = mid + 1 else: - a[mid], a[high] = a[high], a[mid] + sequence[mid], sequence[high] = sequence[high], sequence[mid] high = high - 1 - return a + return sequence if __name__ == "__main__": from doctest import testmod testmod() - + assert dnf_sort([1, 0, 0, 2, 1],5) == [0,0,1, 1, 2] + assert dnf_sort([0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1],12) == [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2] user_input = input("Enter numbers separated by a comma:\n").strip() unsorted = [int(item) for item in user_input.split(",")] -print(dnf_sort(unsorted,len(unsorted))) +print(f"{dnf_sort(unsorted,len(unsorted))}") From 7ace14414ddb385f7489f8b6a8d332181824ca66 Mon Sep 17 00:00:00 2001 From: Kiran Hipparagi <49370990+KiranHipparagi@users.noreply.github.com> Date: Fri, 20 Aug 2021 10:29:35 +0530 Subject: [PATCH 04/12] Added test cases --- sorts/dnf_sort.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sorts/dnf_sort.py b/sorts/dnf_sort.py index 98395d05451a..513c9fbb652a 100644 --- a/sorts/dnf_sort.py +++ b/sorts/dnf_sort.py @@ -48,9 +48,9 @@ def dnf_sort( sequence:list, arr_size:int)->list: return sequence if __name__ == "__main__": - from doctest import testmod + import doctest - testmod() + doctest.testmod() assert dnf_sort([1, 0, 0, 2, 1],5) == [0,0,1, 1, 2] assert dnf_sort([0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1],12) == [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2] From c17470c50ab4a65f499391532bbca5e2777ce3a4 Mon Sep 17 00:00:00 2001 From: Kiran Hipparagi <49370990+KiranHipparagi@users.noreply.github.com> Date: Fri, 20 Aug 2021 14:00:38 +0530 Subject: [PATCH 05/12] Added doctest cases --- sorts/dnf_sort.py | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/sorts/dnf_sort.py b/sorts/dnf_sort.py index 513c9fbb652a..ac2446df331d 100644 --- a/sorts/dnf_sort.py +++ b/sorts/dnf_sort.py @@ -3,7 +3,15 @@ Dutch National Flag algorithm is a popular algorithm originally designed by Edsger Dijkstra. It is the most optimal sort for 3 unique values (eg. 0,1,2) in an Array. dnf sort can sort an array of n size with [0<=a[i]<=2] at guaranteed O(n) complexity in a single pass. - + +The flag of the Netherlands consists of three colors: white, red, and blue. +The task is to randomly arrange balls of white, red, and blue in such a way that balls of the same color are placed together. +For DNF (Dutch National Flag), we sort an array of 0, 1, and 2's in linear time that does not consume any extra space. +We have to keep in mind that this algorithm can be implemented only on an array that has three unique elements. + +1)Time complexity is O(n). +2)Space complexity is O(1). + More info on: https://en.wikipedia.org/wiki/Dutch_national_flag_problem For doctests run following command: @@ -19,7 +27,10 @@ # Python program to sort an array with # 0, 1 and 2 in a single pass - +red = 0 # In this case this is the first color of the flag. +white = 1 # This is the second color of the flag. +blue = 2 # This is the third color of the flag. +colors = [red, white, blue] # Function to sort array def dnf_sort( sequence:list, arr_size:int)->list: """ @@ -32,19 +43,26 @@ def dnf_sort( sequence:list, arr_size:int)->list: Input: dnf_sort([0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1],12) Output: [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2] """ + if sequence is None: return + if len(sequence) <= 1: return low = 0 high = arr_size - 1 mid = 0 while mid <= high: - if sequence[mid] == 0: + if sequence[mid] == colors[0]: sequence[low], sequence[mid] = sequence[mid], sequence[low] low = low + 1 mid = mid + 1 - elif sequence[mid] == 1: + continue + if sequence[mid] == colors[1]: mid = mid + 1 - else: + continue + if sequence[mid] == colors[2]: sequence[mid], sequence[high] = sequence[high], sequence[mid] high = high - 1 + continue + + raise ValueError(f"The elements inside the sequence must contains only {colors} values") return sequence if __name__ == "__main__": From a812c3894c1b88c37ef703b4b02f15582db1ee94 Mon Sep 17 00:00:00 2001 From: Kiran Hipparagi Date: Tue, 31 Aug 2021 16:51:24 +0530 Subject: [PATCH 06/12] Update sorts/dnf_sort.py --- sorts/dnf_sort.py | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/sorts/dnf_sort.py b/sorts/dnf_sort.py index ac2446df331d..63bda8fab7ee 100644 --- a/sorts/dnf_sort.py +++ b/sorts/dnf_sort.py @@ -15,8 +15,7 @@ More info on: https://en.wikipedia.org/wiki/Dutch_national_flag_problem For doctests run following command: -python -m doctest -v dnf_sort.py -or + python3 -m doctest -v dnf_sort.py For manual testing run: @@ -27,15 +26,15 @@ # Python program to sort an array with # 0, 1 and 2 in a single pass -red = 0 # In this case this is the first color of the flag. -white = 1 # This is the second color of the flag. -blue = 2 # This is the third color of the flag. -colors = [red, white, blue] +red = 0 # In this case this is the first color of the flag. +white = 1 # This is the second color of the flag. +blue = 2 # This is the third color of the flag. +colors = (red, white, blue) # Function to sort array -def dnf_sort( sequence:list, arr_size:int)->list: +def dutch_national_flag_sort( sequence: list) -> list: """ - Pure implementation of dnf sort algorithm in Python - :param data: 3 unique values (eg. 0,1,2) in an Array + Pure Python implementation of Dutch National Flag sort algorithm + :param data: 3 unique integer values (e.g., 0, 1, 2) in an sequence :return: the same collection in ascending order Examples: Input: dnf_sort([2, 1, 0, 0,1, 2],6) @@ -43,23 +42,26 @@ def dnf_sort( sequence:list, arr_size:int)->list: Input: dnf_sort([0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1],12) Output: [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2] """ - if sequence is None: return - if len(sequence) <= 1: return + seq_size = len(sequence) # length of sequence + if sequence is None: + return [] + if len(sequence) <= 1: + return [] low = 0 - high = arr_size - 1 + high = seq_size - 1 mid = 0 while mid <= high: if sequence[mid] == colors[0]: sequence[low], sequence[mid] = sequence[mid], sequence[low] - low = low + 1 - mid = mid + 1 + low += 1 + mid += 1 continue if sequence[mid] == colors[1]: - mid = mid + 1 + mid += 1 continue if sequence[mid] == colors[2]: sequence[mid], sequence[high] = sequence[high], sequence[mid] - high = high - 1 + high -= 1 continue raise ValueError(f"The elements inside the sequence must contains only {colors} values") @@ -69,9 +71,6 @@ def dnf_sort( sequence:list, arr_size:int)->list: import doctest doctest.testmod() - - assert dnf_sort([1, 0, 0, 2, 1],5) == [0,0,1, 1, 2] - assert dnf_sort([0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1],12) == [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2] user_input = input("Enter numbers separated by a comma:\n").strip() unsorted = [int(item) for item in user_input.split(",")] -print(f"{dnf_sort(unsorted,len(unsorted))}") + print(f"{dutch_national_flag_sort(unsorted)}") From ea5b6bbb3ae2050901c7abaab3a6edd2bb77a54c Mon Sep 17 00:00:00 2001 From: Kiran Hipparagi Date: Tue, 31 Aug 2021 17:12:58 +0530 Subject: [PATCH 07/12] Added doctest for dutch_national_flag_sort sorts/dnf_sort.py --- sorts/dnf_sort.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sorts/dnf_sort.py b/sorts/dnf_sort.py index 63bda8fab7ee..a28f323929fd 100644 --- a/sorts/dnf_sort.py +++ b/sorts/dnf_sort.py @@ -37,9 +37,9 @@ def dutch_national_flag_sort( sequence: list) -> list: :param data: 3 unique integer values (e.g., 0, 1, 2) in an sequence :return: the same collection in ascending order Examples: - Input: dnf_sort([2, 1, 0, 0,1, 2],6) + Input: dutch_national_flag_sort([2, 1, 0, 0,1, 2]) Output: [0, 0, 1, 1, 2, 2] - Input: dnf_sort([0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1],12) + Input: dutch_national_flag_sort([0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1]) Output: [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2] """ seq_size = len(sequence) # length of sequence From bc9cdc6dd36eae051a0af42714e2570cc8ab9d80 Mon Sep 17 00:00:00 2001 From: Kiran Hipparagi Date: Tue, 31 Aug 2021 17:28:51 +0530 Subject: [PATCH 08/12] Update sorts/dnf_sort.py --- sorts/dnf_sort.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/sorts/dnf_sort.py b/sorts/dnf_sort.py index a28f323929fd..b5a1806cd3d5 100644 --- a/sorts/dnf_sort.py +++ b/sorts/dnf_sort.py @@ -55,22 +55,21 @@ def dutch_national_flag_sort( sequence: list) -> list: sequence[low], sequence[mid] = sequence[mid], sequence[low] low += 1 mid += 1 - continue - if sequence[mid] == colors[1]: + elif sequence[mid] == colors[1]: mid += 1 - continue - if sequence[mid] == colors[2]: + + elif sequence[mid] == colors[2]: sequence[mid], sequence[high] = sequence[high], sequence[mid] high -= 1 - continue - - raise ValueError(f"The elements inside the sequence must contains only {colors} values") + else: + raise ValueError(f"The elements inside the sequence must contains only {colors} values") return sequence if __name__ == "__main__": import doctest doctest.testmod() + user_input = input("Enter numbers separated by a comma:\n").strip() unsorted = [int(item) for item in user_input.split(",")] print(f"{dutch_national_flag_sort(unsorted)}") From c35debbebcde4095d166e44e865c63c16598c1a4 Mon Sep 17 00:00:00 2001 From: Kiran Hipparagi Date: Tue, 31 Aug 2021 17:35:52 +0530 Subject: [PATCH 09/12] Added doctest for the function dutch_national_flag_sort --- sorts/dnf_sort.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sorts/dnf_sort.py b/sorts/dnf_sort.py index b5a1806cd3d5..4f7172c67f46 100644 --- a/sorts/dnf_sort.py +++ b/sorts/dnf_sort.py @@ -37,10 +37,10 @@ def dutch_national_flag_sort( sequence: list) -> list: :param data: 3 unique integer values (e.g., 0, 1, 2) in an sequence :return: the same collection in ascending order Examples: - Input: dutch_national_flag_sort([2, 1, 0, 0,1, 2]) - Output: [0, 0, 1, 1, 2, 2] - Input: dutch_national_flag_sort([0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1]) - Output: [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2] + >>> dutch_national_flag_sort([2, 1, 0, 0, 1, 2]) + [0, 0, 1, 1, 2, 2] + >>> dutch_national_flag_sort([0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1]) + [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2] """ seq_size = len(sequence) # length of sequence if sequence is None: From 92bdfc50a9a8f87d390b5cc34b652c48c808e565 Mon Sep 17 00:00:00 2001 From: Kiran Hipparagi Date: Tue, 31 Aug 2021 20:05:48 +0530 Subject: [PATCH 10/12] update file as per black code formatter --- sorts/dnf_sort.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/sorts/dnf_sort.py b/sorts/dnf_sort.py index 4f7172c67f46..619507d2cc5a 100644 --- a/sorts/dnf_sort.py +++ b/sorts/dnf_sort.py @@ -31,7 +31,7 @@ blue = 2 # This is the third color of the flag. colors = (red, white, blue) # Function to sort array -def dutch_national_flag_sort( sequence: list) -> list: +def dutch_national_flag_sort(sequence: list) -> list: """ Pure Python implementation of Dutch National Flag sort algorithm :param data: 3 unique integer values (e.g., 0, 1, 2) in an sequence @@ -42,7 +42,7 @@ def dutch_national_flag_sort( sequence: list) -> list: >>> dutch_national_flag_sort([0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1]) [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2] """ - seq_size = len(sequence) # length of sequence + seq_size = len(sequence) # length of sequence if sequence is None: return [] if len(sequence) <= 1: @@ -62,9 +62,12 @@ def dutch_national_flag_sort( sequence: list) -> list: sequence[mid], sequence[high] = sequence[high], sequence[mid] high -= 1 else: - raise ValueError(f"The elements inside the sequence must contains only {colors} values") + raise ValueError( + f"The elements inside the sequence must contains only {colors} values" + ) return sequence + if __name__ == "__main__": import doctest From c19022c8c5840d96133a8c595054d8f56b8e0352 Mon Sep 17 00:00:00 2001 From: Kiran Hipparagi Date: Tue, 31 Aug 2021 21:59:28 +0530 Subject: [PATCH 11/12] Update dnf_sort.py --- sorts/dnf_sort.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorts/dnf_sort.py b/sorts/dnf_sort.py index 619507d2cc5a..5e86cf5e16b8 100644 --- a/sorts/dnf_sort.py +++ b/sorts/dnf_sort.py @@ -35,7 +35,7 @@ def dutch_national_flag_sort(sequence: list) -> list: """ Pure Python implementation of Dutch National Flag sort algorithm :param data: 3 unique integer values (e.g., 0, 1, 2) in an sequence - :return: the same collection in ascending order + :return: The same collection in ascending order Examples: >>> dutch_national_flag_sort([2, 1, 0, 0, 1, 2]) [0, 0, 1, 1, 2, 2] From e08603ec313fd1d9500143ba67eaca68ebb209e4 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 31 Aug 2021 22:01:34 +0200 Subject: [PATCH 12/12] Update and rename dnf_sort.py to dutch_national_flag_sort.py --- sorts/dnf_sort.py | 78 ----------------------- sorts/dutch_national_flag_sort.py | 100 ++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 78 deletions(-) delete mode 100644 sorts/dnf_sort.py create mode 100644 sorts/dutch_national_flag_sort.py diff --git a/sorts/dnf_sort.py b/sorts/dnf_sort.py deleted file mode 100644 index 5e86cf5e16b8..000000000000 --- a/sorts/dnf_sort.py +++ /dev/null @@ -1,78 +0,0 @@ -""" -Pure implementation of Dutch national flag problem(dnf) sort algorithm in Python -Dutch National Flag algorithm is a popular algorithm originally designed by Edsger Dijkstra. -It is the most optimal sort for 3 unique values (eg. 0,1,2) in an Array. -dnf sort can sort an array of n size with [0<=a[i]<=2] at guaranteed O(n) complexity in a single pass. - -The flag of the Netherlands consists of three colors: white, red, and blue. -The task is to randomly arrange balls of white, red, and blue in such a way that balls of the same color are placed together. -For DNF (Dutch National Flag), we sort an array of 0, 1, and 2's in linear time that does not consume any extra space. -We have to keep in mind that this algorithm can be implemented only on an array that has three unique elements. - -1)Time complexity is O(n). -2)Space complexity is O(1). - -More info on: https://en.wikipedia.org/wiki/Dutch_national_flag_problem - -For doctests run following command: - -python3 -m doctest -v dnf_sort.py - -For manual testing run: -python dnf_sort.py - -""" - - -# Python program to sort an array with -# 0, 1 and 2 in a single pass -red = 0 # In this case this is the first color of the flag. -white = 1 # This is the second color of the flag. -blue = 2 # This is the third color of the flag. -colors = (red, white, blue) -# Function to sort array -def dutch_national_flag_sort(sequence: list) -> list: - """ - Pure Python implementation of Dutch National Flag sort algorithm - :param data: 3 unique integer values (e.g., 0, 1, 2) in an sequence - :return: The same collection in ascending order - Examples: - >>> dutch_national_flag_sort([2, 1, 0, 0, 1, 2]) - [0, 0, 1, 1, 2, 2] - >>> dutch_national_flag_sort([0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1]) - [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2] - """ - seq_size = len(sequence) # length of sequence - if sequence is None: - return [] - if len(sequence) <= 1: - return [] - low = 0 - high = seq_size - 1 - mid = 0 - while mid <= high: - if sequence[mid] == colors[0]: - sequence[low], sequence[mid] = sequence[mid], sequence[low] - low += 1 - mid += 1 - elif sequence[mid] == colors[1]: - mid += 1 - - elif sequence[mid] == colors[2]: - sequence[mid], sequence[high] = sequence[high], sequence[mid] - high -= 1 - else: - raise ValueError( - f"The elements inside the sequence must contains only {colors} values" - ) - return sequence - - -if __name__ == "__main__": - import doctest - - doctest.testmod() - - user_input = input("Enter numbers separated by a comma:\n").strip() - unsorted = [int(item) for item in user_input.split(",")] - print(f"{dutch_national_flag_sort(unsorted)}") diff --git a/sorts/dutch_national_flag_sort.py b/sorts/dutch_national_flag_sort.py new file mode 100644 index 000000000000..79afefa73afe --- /dev/null +++ b/sorts/dutch_national_flag_sort.py @@ -0,0 +1,100 @@ +""" +A pure implementation of Dutch national flag (DNF) sort algorithm in Python. +Dutch National Flag algorithm is an algorithm originally designed by Edsger Dijkstra. +It is the most optimal sort for 3 unique values (eg. 0, 1, 2) in a sequence. DNF can +sort a sequence of n size with [0 <= a[i] <= 2] at guaranteed O(n) complexity in a +single pass. + +The flag of the Netherlands consists of three colors: white, red, and blue. +The task is to randomly arrange balls of white, red, and blue in such a way that balls +of the same color are placed together. DNF sorts a sequence of 0, 1, and 2's in linear +time that does not consume any extra space. This algorithm can be implemented only on +a sequence that contains three unique elements. + +1) Time complexity is O(n). +2) Space complexity is O(1). + +More info on: https://en.wikipedia.org/wiki/Dutch_national_flag_problem + +For doctests run following command: +python3 -m doctest -v dutch_national_flag_sort.py + +For manual testing run: +python dnf_sort.py +""" + + +# Python program to sort a sequence containing only 0, 1 and 2 in a single pass. +red = 0 # The first color of the flag. +white = 1 # The second color of the flag. +blue = 2 # The third color of the flag. +colors = (red, white, blue) + + +def dutch_national_flag_sort(sequence: list) -> list: + """ + A pure Python implementation of Dutch National Flag sort algorithm. + :param data: 3 unique integer values (e.g., 0, 1, 2) in an sequence + :return: The same collection in ascending order + + >>> dutch_national_flag_sort([]) + [] + >>> dutch_national_flag_sort([0]) + [0] + >>> dutch_national_flag_sort([2, 1, 0, 0, 1, 2]) + [0, 0, 1, 1, 2, 2] + >>> dutch_national_flag_sort([0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1]) + [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2] + >>> dutch_national_flag_sort("abacab") + Traceback (most recent call last): + ... + ValueError: The elements inside the sequence must contains only (0, 1, 2) values + >>> dutch_national_flag_sort("Abacab") + Traceback (most recent call last): + ... + ValueError: The elements inside the sequence must contains only (0, 1, 2) values + >>> dutch_national_flag_sort([3, 2, 3, 1, 3, 0, 3]) + Traceback (most recent call last): + ... + ValueError: The elements inside the sequence must contains only (0, 1, 2) values + >>> dutch_national_flag_sort([-1, 2, -1, 1, -1, 0, -1]) + Traceback (most recent call last): + ... + ValueError: The elements inside the sequence must contains only (0, 1, 2) values + >>> dutch_national_flag_sort([1.1, 2, 1.1, 1, 1.1, 0, 1.1]) + Traceback (most recent call last): + ... + ValueError: The elements inside the sequence must contains only (0, 1, 2) values + """ + if not sequence: + return [] + if len(sequence) == 1: + return list(sequence) + low = 0 + high = len(sequence) - 1 + mid = 0 + while mid <= high: + if sequence[mid] == colors[0]: + sequence[low], sequence[mid] = sequence[mid], sequence[low] + low += 1 + mid += 1 + elif sequence[mid] == colors[1]: + mid += 1 + elif sequence[mid] == colors[2]: + sequence[mid], sequence[high] = sequence[high], sequence[mid] + high -= 1 + else: + raise ValueError( + f"The elements inside the sequence must contains only {colors} values" + ) + return sequence + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + + user_input = input("Enter numbers separated by commas:\n").strip() + unsorted = [int(item.strip()) for item in user_input.split(",")] + print(f"{dutch_national_flag_sort(unsorted)}")