fix(arrays): return -1 from sorted_last_index_of on empty array

sorted_last_index returns 0 for an empty array (and for any value smaller
than every element), so index becomes -1. The guard only bounded index
from above, so array[-1] was evaluated on an empty list and raised
IndexError. Add the lower-bound check so a negative index is reported as
not found, matching sorted_index_of.
This commit is contained in:
dualfroz committed 2026-09-04 09:17:47 +02:00
1 parent 40107f4ad7
commit 9194f6d3ad
3 files changed
+79 -1

No files matched your search

+4
View File
@@ -656,6 +656,10 @@ def test_sorted_last_index_by(case, expected):
[
([2, 3, 4, 10, 10], 10, 4),
([10, 10, 4, 2, 3], 11, -1),
# Empty array must return -1, not raise IndexError.
([], 10, -1),
# Value lower than every element also returns -1 without indexing [-1].
([2, 3, 4], 1, -1),
],
)
def test_sorted_last_index_of(array, value, expected):