Skip to content

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 9 commits into from
Oct 18, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove Trailing Whitespace
  • Loading branch information
JimmyRaper committed Oct 18, 2020
commit 819bdac947ce30071f9e11467f5f6fd19cde9473
12 changes: 6 additions & 6 deletions sorts/patience_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]


Expand Down Expand Up @@ -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(",")]
Expand Down