typing: bound recursive insertion and quick sorts to Comparable protocol - #15471
Closed
Silverbullets1 wants to merge 1 commit into
Closed
Silverbullets1 wants to merge 1 commit into
Silverbullets1 wants to merge 1 commit into
Conversation
- recursive_quick_sort.py: Any -> Comparable Protocol with TypeVar bound - recursive_insertion_sort.py: bind insert_next TypeVar, __lt__-only compare - doctests + test_sorts.py: strings/floats succeed, mixed types raise TypeError Part of TheAlgorithms#15234
37 of 41 tasks
Closing this pull request as invalid@Silverbullets1, this pull request is being closed as none of the checkboxes have been marked. It is important that you go through the checklist and mark the ones relevant to this pull request. Please read the Contributing guidelines. If you're facing any problem on how to mark a checkbox, please read the following instructions:
NOTE: Only |
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.
Part of #15234
This PR covers the
recursive_insertion_sort.pyandrecursive_quick_sort.pycheckboxes of the umbrella issue.Changes
sorts/recursive_quick_sort.py
Anyitems with theComparableProtocol +TypeVarbound pattern (quick_sort[T: Comparable](data: list[T]) -> list[T]).__lt__(not data[0] < e/data[0] < e), matching the other checked-in comparison sorts.quick_sort([1, "a"])raisesTypeError.sorts/recursive_insertion_sort.py
insert_nextwas missing its TypeVar bound (Pyright flagged the<operator) — nowinsert_next[T: Comparable], matchingrec_insertion_sort.__lt__(self, other: Any)→__lt__(self, other: object)in the Protocol, per the issue's reference pattern.collection[index - 1] <= collection[index](which needs__le__) tonot collection[index] < collection[index - 1], so the algorithm relies on__lt__alone.rec_insertion_sort([1, "a"], 2)raisesTypeError.tests/test_sorts.py
recursive_quick_sortto the shared parametrized battery (test_sort_matches_builtinacross all existing cases) and totest_sort_rejects_non_comparable_items.test_recursive_quick_sort_mixed_types: strings/floats succeed, mixed[1, "a"]raisesTypeError.Verification
Comments on scope: kept to the two files named in the issue's checklist; no drive-by refactors. The
__lt__-only comparison preserves exact behavior for previously-working inputs (equal elements still compare correctly, e.g.[3, 2, 3]doctest unchanged).Best,
Sam | Lead Engineer
DevilX Digital Labs
https://devilx-solution.vercel.app/