Skip to content

Commit 3a65a15

Browse files
committed
change first occurrence algorithm
1 parent e87a0a0 commit 3a65a15

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

search/first_occurrence.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
def first_occurrence(array: list, target: Any) -> int:
1111
low, high = 0, len(array) - 1
12-
while low < high:
12+
while low <= high:
1313
middle = (low + high) // 2
1414
if array[middle] < target:
1515
low = middle + 1
@@ -23,3 +23,5 @@ def first_occurrence(array: list, target: Any) -> int:
2323
if __name__ == "__main__":
2424
print(first_occurrence([2, 2, 2, 3, 3, 4, 4, 5, 5, 5], 3) == 3)
2525
print(first_occurrence([2, 2, 2, 3, 3, 4, 4, 5, 5, 5], 2) == 0)
26+
print(first_occurrence([2, 2, 2, 3, 3, 4, 4, 5, 5, 5], 4) == 5)
27+
print(first_occurrence([2, 2, 2, 3, 3, 4, 4, 5, 5, 5], 5) == 7)

0 commit comments

Comments
 (0)