Skip to content

Commit 07bcf1a

Browse files
Fix stalin_sort raising IndexError on an empty list
The function read sequence[0] unconditionally. Return an empty list for empty input and add doctests for the empty and single-element cases. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
1 parent 2acda52 commit 07bcf1a

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

‎sorts/stalin_sort.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,16 @@ def stalin_sort(sequence: list[int]) -> list[int]:
3232
3333
>>> stalin_sort([1, 2, 8, 7, 6])
3434
[1, 2, 8]
35+
36+
>>> stalin_sort([])
37+
[]
38+
39+
>>> stalin_sort([7])
40+
[7]
3541
"""
42+
if not sequence:
43+
return []
44+
3645
result = [sequence[0]]
3746
for element in sequence[1:]:
3847
if element >= result[-1]:

0 commit comments

Comments
 (0)