fix(sorts): make smoothsort generic over Comparable items - #15441
Open
wanjinhao1 wants to merge 1 commit into
Open
wanjinhao1 wants to merge 1 commit into
wanjinhao1 wants to merge 1 commit into
Conversation
This branch has not been deployed
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Describe your change
Checklist
Part of #15234 (does not close the tracking issue).
What
Comparableprotocol (__lt__only) and aTypeVarbound sosmoothsort(and its helpers_sift/_trinkle) accept any list of mutually comparable items (list[T]) instead oflist[int]._siftand_trinkleto use only<(the protocol guarantees nothing else):>=becomesnot (a < b)anda > bbecomesb < a, preserving stability semantics for total orders.TypeErrorfailure mode (smoothsort([1, "a"])).smoothsortin the sharedSORTSbattery intests/test_sorts.py, which also covers the non-comparable rejection case.Why
smoothsort.pyis one of the remaining unchecked comparison sorts in the #15234 checklist, with no open PR and no assignee.Validation
python3 -m doctest sorts/smoothsort.py— all doctests pass (including the new string/float/TypeError cases)python3 sorts/smoothsort.py— all doctests plus the built-in 5,000 random trials passpython3 -m pytest tests/test_sorts.py -q— 394 passed (smoothsort now runs across the full shared CASES battery, includingPerson/Dogdataclasses, and the[1, "a"]rejection test)Note: one algorithm per PR per the tracker guidance. This PR was completed with AI assistance under the supervision of the account owner (@wanjinhao1) — happy to address any review feedback.