Skip to content

fix: update patience sort to support mixed data types - #15479

Closed
Obed-727 wants to merge 5 commits into
TheAlgorithms:masterfrom
Obed-727:patch-1
Closed

Obed-727 wants to merge 5 commits into
TheAlgorithms:masterfrom
Obed-727:patch-1

Conversation

@Obed-727

@Obed-727 Obed-727 commented Oct 2, 2026

Copy link
Copy Markdown

Describe your change

The changes I made include:

1.I updated the existing patience_sort() function and its documentation.
2. I added mixed_patience_sort() to support sorting collections containing integers, floats, and strings.
3. I added convert_item() to convert user input into integers, floats, or strings.
4. I updated the manual input section to accept integers, floats, and strings.
5. I added comments to make the main parts of the implementation easier to understand.
6. I added examples for the new functionality.
7. I kept the original patience sort algorithm as the foundation of the implementation.

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • Documentation change?

Checklist

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues, then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

The previous code was only able to receive integers but crashes when you included string and float. Now the new changes receives strings, floats, and integers without crashing.
…ort them.

The code receives integers, strings, and floats. After that it sort them in this order:
1. Numbers are sorted first, followed by alphabets.
2. Integers and floats are sorted according to their magnitude.
3. Strings are sorted in alphabetical order.
I reviewed and revised the changes from the previous commits and made sure that they follow the CONTRIBUTING.md guidelines. The revisions I made include:

1.Correcting and expanding doctests.
2. Adding missing type hints and docstrings.
3. Handling invalid input with ValueError.
4. Improving variable naming and code readability.
5. Cleaning up formatting and spacing.
6. Maintaining the original patience sort implementation and the  added mixed integer, float, and string functionality.
@algorithms-keeper algorithms-keeper Bot added awaiting reviews This PR is ready to be reviewed merge conflicts Open a new PR or rebase on the latest commit enhancement This PR modified some existing files labels Oct 2, 2026
@cclauss

cclauss commented Oct 2, 2026 •

Copy link
Copy Markdown
Member

Please rebase to the current master branch to fix git merge conflicts.

Comment thread sorts/patience_sort.py Outdated
user_input = input("Enter numbers separated by a comma:\n").strip()
unsorted = [int(item) for item in user_input.split(",")]
print(patience_sort(unsorted))
user_input: str| int|float = input("Enter integers, floats, and strings separated by commas:\n").strip()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

input() always returns a string. It never returns an int or a float.

We do not need type hints everywhere. Python can figure out what the types are on its own.

We only need type hints on function parameters and function returns.

@algorithms-keeper algorithms-keeper Bot removed the merge conflicts Open a new PR or rebase on the latest commit label Oct 2, 2026
@algorithms-keeper algorithms-keeper Bot added tests are failing Do not merge until tests pass and removed tests are failing Do not merge until tests pass labels Oct 2, 2026
@cclauss

cclauss commented Oct 2, 2026

Copy link
Copy Markdown
Member

This algorithm was already modified to match the approach discussed in:

@cclauss cclauss closed this Oct 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants