From 0cd6ba4db4512fd739510a3ffb92f23adc7d2e08 Mon Sep 17 00:00:00 2001 From: jvnascimento Date: Sat, 17 Oct 2020 23:32:28 -0300 Subject: [PATCH 1/9] Add Patience Sort --- DIRECTORY.md | 1 + sorts/patience_sort.py | 59 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 sorts/patience_sort.py diff --git a/DIRECTORY.md b/DIRECTORY.md index 064a4da23f17..792e2657c0a5 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -712,6 +712,7 @@ * [Odd Even Transposition Parallel](https://github.com/TheAlgorithms/Python/blob/master/sorts/odd_even_transposition_parallel.py) * [Odd Even Transposition Single Threaded](https://github.com/TheAlgorithms/Python/blob/master/sorts/odd_even_transposition_single_threaded.py) * [Pancake Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/pancake_sort.py) + * [Patience Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/patience_sort.py) * [Pigeon Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/pigeon_sort.py) * [Pigeonhole Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/pigeonhole_sort.py) * [Quick Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/quick_sort.py) diff --git a/sorts/patience_sort.py b/sorts/patience_sort.py new file mode 100644 index 000000000000..de02c061055f --- /dev/null +++ b/sorts/patience_sort.py @@ -0,0 +1,59 @@ +from functools import total_ordering +from bisect import bisect_left +from heapq import merge + +""" +A pure Python implementation of the patience sort algorithm + +This algorithm is based on the card game patience + +For doctests run following command: +python3 -m doctest -v patience_sort.py + +For manual testing run: +python3 patience_sort.py +""" + +@total_ordering +class Stack(list): + def __lt__(self, other): return self[-1] < other[-1] + def __eq__(self, other): return self[-1] == other[-1] + +def patience_sort(collection list) -> list: + """A pure implementation of quick sort algorithm in Python + + :param collection: some mutable ordered collection with heterogeneous + comparable items inside + :return: the same collection ordered by ascending + + Examples: + >>> patience_sort([1, 9, 5, 21, 17, 6]) + [1, 5, 6, 9, 17, 21] + + >>> patience_sort([]) + [] + + >>> patience_sort([-3, -17, -48]) + [-48, -17, -3] + """ + stacks = [] + # sort into stacks + for element in collection: + new_stacks = Stack([element]) + i = bisect_left(stacks, new_stacks) + if i != len(stacks): + stacks[i].append(element) + else: + stacks.append(new_stacks) + + # use a heap-based merge to merge stack efficiently + collection[:] = merge(*[reversed(stack) for stack in stacks]) + + return collection + + +if __name__ == "__main__": + user_input = input("Enter numbers separated by a comma:\n").strip() + unsorted = [int(item) for item in user_input.split(",")] + print(patience_sort(unsorted)) + \ No newline at end of file From 4728f508cff20aa12f6ec4f899701b27a7e8bcc4 Mon Sep 17 00:00:00 2001 From: jvnascimento Date: Sat, 17 Oct 2020 23:50:04 -0300 Subject: [PATCH 2/9] fix code for pre-commit --- sorts/patience_sort.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/sorts/patience_sort.py b/sorts/patience_sort.py index de02c061055f..7f5bc3174f5b 100644 --- a/sorts/patience_sort.py +++ b/sorts/patience_sort.py @@ -14,10 +14,12 @@ python3 patience_sort.py """ + @total_ordering class Stack(list): - def __lt__(self, other): return self[-1] < other[-1] - def __eq__(self, other): return self[-1] == other[-1] + def __lt__(self, other): return self[-1] < other[-1] + def __eq__(self, other): return self[-1] == other[-1] + def patience_sort(collection list) -> list: """A pure implementation of quick sort algorithm in Python @@ -48,7 +50,6 @@ def patience_sort(collection list) -> list: # use a heap-based merge to merge stack efficiently collection[:] = merge(*[reversed(stack) for stack in stacks]) - return collection From 0998ba74bc2714aa678f294cae6cca0eb6eeef28 Mon Sep 17 00:00:00 2001 From: jvnascimento Date: Sat, 17 Oct 2020 23:58:34 -0300 Subject: [PATCH 3/9] Fix params def --- sorts/patience_sort.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/sorts/patience_sort.py b/sorts/patience_sort.py index 7f5bc3174f5b..8c0865f5aa08 100644 --- a/sorts/patience_sort.py +++ b/sorts/patience_sort.py @@ -17,11 +17,13 @@ @total_ordering class Stack(list): - def __lt__(self, other): return self[-1] < other[-1] - def __eq__(self, other): return self[-1] == other[-1] + def __lt__(self, other): + return self[-1] < other[-1] + def __eq__(self, other): + return self[-1] == other[-1] - -def patience_sort(collection list) -> list: + +def patience_sort(collection: list) -> list: """A pure implementation of quick sort algorithm in Python :param collection: some mutable ordered collection with heterogeneous @@ -56,5 +58,4 @@ def patience_sort(collection list) -> list: if __name__ == "__main__": user_input = input("Enter numbers separated by a comma:\n").strip() unsorted = [int(item) for item in user_input.split(",")] - print(patience_sort(unsorted)) - \ No newline at end of file + print(patience_sort(unsorted)) \ No newline at end of file From f5df57f2c448b89db5556c15bffae2403f5aa05a Mon Sep 17 00:00:00 2001 From: jvnascimento Date: Sun, 18 Oct 2020 00:17:00 -0300 Subject: [PATCH 4/9] Adding new line at end of file --- sorts/patience_sort.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorts/patience_sort.py b/sorts/patience_sort.py index 8c0865f5aa08..24872f3dad68 100644 --- a/sorts/patience_sort.py +++ b/sorts/patience_sort.py @@ -58,4 +58,4 @@ def patience_sort(collection: list) -> list: if __name__ == "__main__": user_input = input("Enter numbers separated by a comma:\n").strip() unsorted = [int(item) for item in user_input.split(",")] - print(patience_sort(unsorted)) \ No newline at end of file + print(patience_sort(unsorted)) From 819bdac947ce30071f9e11467f5f6fd19cde9473 Mon Sep 17 00:00:00 2001 From: jvnascimento Date: Sun, 18 Oct 2020 00:30:57 -0300 Subject: [PATCH 5/9] Remove Trailing Whitespace --- sorts/patience_sort.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/sorts/patience_sort.py b/sorts/patience_sort.py index 24872f3dad68..b5f95a60dafe 100644 --- a/sorts/patience_sort.py +++ b/sorts/patience_sort.py @@ -13,13 +13,13 @@ For manual testing run: python3 patience_sort.py """ - + @total_ordering class Stack(list): - def __lt__(self, other): + def __lt__(self, other): return self[-1] < other[-1] - def __eq__(self, other): + def __eq__(self, other): return self[-1] == other[-1] @@ -49,12 +49,12 @@ def patience_sort(collection: list) -> list: stacks[i].append(element) else: stacks.append(new_stacks) - + # use a heap-based merge to merge stack efficiently collection[:] = merge(*[reversed(stack) for stack in stacks]) return collection - - + + if __name__ == "__main__": user_input = input("Enter numbers separated by a comma:\n").strip() unsorted = [int(item) for item in user_input.split(",")] From 47378c9376136ae1a04832ff1124cdaff0b09551 Mon Sep 17 00:00:00 2001 From: jvnascimento Date: Sun, 18 Oct 2020 11:32:32 -0300 Subject: [PATCH 6/9] Adding space between the methods of the Stack class --- sorts/patience_sort.py | 1 + 1 file changed, 1 insertion(+) diff --git a/sorts/patience_sort.py b/sorts/patience_sort.py index b5f95a60dafe..285f4a5778eb 100644 --- a/sorts/patience_sort.py +++ b/sorts/patience_sort.py @@ -19,6 +19,7 @@ class Stack(list): def __lt__(self, other): return self[-1] < other[-1] + def __eq__(self, other): return self[-1] == other[-1] From 9ee58dc7538eab7868f0f8601ac26b77418c98c1 Mon Sep 17 00:00:00 2001 From: jvnascimento Date: Sun, 18 Oct 2020 11:36:12 -0300 Subject: [PATCH 7/9] Removing Trailing Whitespace --- sorts/patience_sort.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorts/patience_sort.py b/sorts/patience_sort.py index 285f4a5778eb..6cd872e06eb6 100644 --- a/sorts/patience_sort.py +++ b/sorts/patience_sort.py @@ -19,7 +19,7 @@ class Stack(list): def __lt__(self, other): return self[-1] < other[-1] - + def __eq__(self, other): return self[-1] == other[-1] From 3e4dfe479447cf7004d24a8e5baace7d8e4718e3 Mon Sep 17 00:00:00 2001 From: jvnascimento Date: Sun, 18 Oct 2020 11:40:15 -0300 Subject: [PATCH 8/9] Ordering Imports --- sorts/patience_sort.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorts/patience_sort.py b/sorts/patience_sort.py index 6cd872e06eb6..59b1031ecec2 100644 --- a/sorts/patience_sort.py +++ b/sorts/patience_sort.py @@ -1,5 +1,5 @@ -from functools import total_ordering from bisect import bisect_left +from functools import total_ordering from heapq import merge """ From 8d41a4dd59c08c3a630820e96b6f44f310ed4c29 Mon Sep 17 00:00:00 2001 From: jvnascimento Date: Sun, 18 Oct 2020 15:06:13 -0300 Subject: [PATCH 9/9] Adding url patience sort --- sorts/patience_sort.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sorts/patience_sort.py b/sorts/patience_sort.py index 59b1031ecec2..f4e35d9a0ac6 100644 --- a/sorts/patience_sort.py +++ b/sorts/patience_sort.py @@ -5,6 +5,8 @@ """ A pure Python implementation of the patience sort algorithm +For more information: https://en.wikipedia.org/wiki/Patience_sorting + This algorithm is based on the card game patience For doctests run following command: