sorts: type selection sort for comparable items - #15245
Conversation
|
@priya-sundaram-dev, your review, please. |
| [0] | ||
|
|
||
| >>> selection_sort([2, -3, 0, 5, -1]) | ||
| [-3, -1, 0, 2, 5] |
| >>> selection_sort(["d", "a", "c", "b"]) | ||
| ['a', 'b', 'c', 'd'] | ||
|
|
||
| >>> selection_sort([0, 5, 3, 2, 2]) == sorted([0, 5, 3, 2, 2]) |
|
Sorry about that... I accidentally removed the existing doctest while updating the type annotations. I've restored the original selection-sort doctests, including the |
priya-sundaram-dev
left a comment
There was a problem hiding this comment.
LGTM! ✅ This matches the pattern we settled on in #15234 — a minimal Comparable protocol (just __lt__, which is all selection sort needs), the generic [T: Comparable] type parameter, and MutableSequence[T] in and out. The algorithm body is untouched, and the added string/float doctests are a nice touch. Ran the doctests locally — all 13 pass. Thanks for restoring the original doctests after the earlier slip; looks clean now. 🙌
|
Perfect—restored doctest plus the new comparable-item coverage is exactly right, and the quick turnaround was great. Thanks @HarshRajSinghania, and welcome aboard! 🎉 |
Part of #15234
Make
selection_sortcorrectly typed for any mutually comparable items instead of onlylist[int].Changes:
Comparableprotocol with the existing generic type-parameter pattern.MutableSequence[T].Testing:
python -m doctest -v sorts/selection_sort.py— 7 passedpython -m pytest tests/test_sorts.py -q— 185 passed