Add input validation to cyclic_sort to prevent invalid inputs - #15009
Add input validation to cyclic_sort to prevent invalid inputs#15009Gairola788 wants to merge 5 commits into
Conversation
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
Claudiocli
left a comment
There was a problem hiding this comment.
Input validation seems adequate to constraints
Thank you for reviewing the changes and confirming that the input validation meets the constraints. I really appreciate your time and approval! |
daltino
left a comment
There was a problem hiding this comment.
The validation loop is O(n) extra work before the sort even begins, which is fine, but more importantly it fundamentally changes the contract of the function in a way that's inconsistent with how cyclic sort is typically presented as an educational algorithm. The whole point of cyclic sort is that it handles duplicate detection implicitly — by adding a pre-check that raises on duplicates, you're essentially duplicating the effort the algorithm itself would reveal. More practically, the doctest examples don't cover the new ValueError paths at all, so the new validation code is completely untested per the automated checks. You should add doctests like >>> cyclic_sort([1, 2, 2]) and >>> cyclic_sort([1, 5]) with the expected exceptions. Also, the reformatting of the swap from a one-liner to a multi-line tuple is an unrelated style change that muddies the diff — best to keep that separate.
Describe your change:
Added input validation to the
cyclic_sortalgorithm to prevent invalid inputs from causing infinite loops or incorrect results.The function now raises a
ValueErrorwhen:1ton.Fixes #14898
Checklist: