sorts: type bubble sort for any comparable items - #15240
Conversation
Bound bubble_sort_iterative and bubble_sort_recursive to a Comparable protocol instead of Any, add TypeError doctests, and cover the mixed-type failure in tests/test_sorts.py. Refs TheAlgorithms#15234
Closing this pull request as invalid@HarshRajSinghania, 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 |
priya-sundaram-dev
left a comment
There was a problem hiding this comment.
Nice work @HarshRajSinghania — this faithfully follows the insertion_sort.py reference pattern:
Comparableprotocol + PEP 695[T: Comparable]on both the iterative and recursive variants ✅+IGNORE_EXCEPTION_DETAILdoctest showing theTypeErroron mixed[1, "a"]✅- parametrized
test_bubble_sort_rejects_non_comparable_itemscovering both entry points ✅
Both functions share the same protocol, so mixed-type inputs fail fast exactly like the reference. CI is green and it's mergeable. LGTM from me — deferring to @cclauss for the merge. Thanks for jumping on this one!
Not. |
Updated tests to include binary insertion sort and renamed test function.
for more information, see https://pre-commit.ci
|
@priya-sundaram-dev Thanks! I’ve taken care of the follow-up on the PR in this pass:
Thanks again for the review and the kind words! 🙌 |
Summary
Fixes: #15241
Make
bubble_sort_iterativeandbubble_sort_recursivecorrectly typed and tested for any mutually comparable items, not onlylist[int].Motivation
Requested in #15234. One comparison sort per PR.
Implementation
list[Any]with aComparableprotocol anddef bubble_sort_*[T: Comparable](collection: list[T]) -> list[T], matchinginsertion_sort.py.>).TypeErroron mixed non-comparable input ([1, "a"]).strandfloat.Testing
Ran locally:
python3 -m doctest -v sorts/bubble_sort.py— 42 passed, 0 failed[1, "a"]raisesTypeErrorfor both functionspython3 -cAST parse of both changed files succeededAlso added
test_bubble_sort_rejects_non_comparable_itemsintests/test_sorts.pyfor both functions.Fixes #15234.
Describe your change
Checklist