-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
Add Patience Sort #3469
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add Patience Sort #3469
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0cd6ba4
Add Patience Sort
JimmyRaper 4728f50
fix code for pre-commit
JimmyRaper 0998ba7
Fix params def
JimmyRaper f5df57f
Adding new line at end of file
JimmyRaper 819bdac
Remove Trailing Whitespace
JimmyRaper 47378c9
Adding space between the methods of the Stack class
JimmyRaper 9ee58dc
Removing Trailing Whitespace
JimmyRaper 3e4dfe4
Ordering Imports
JimmyRaper 8d41a4d
Adding url patience sort
JimmyRaper File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from bisect import bisect_left | ||
from functools import total_ordering | ||
from heapq import merge | ||
|
||
""" | ||
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: | ||
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)) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide some url for theory
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added