Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion searches/binary_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,10 @@
midpoint = left + (right - left) // 2
current_item = sorted_collection[midpoint]
if current_item == item:
return midpoint
if midpoint > 0 and sorted_collection[midpoint - 1] == item:
right = midpoint - 1 # Keep searching left
else:
return midpoint

Check failure on line 213 in searches/binary_search.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (W291)

searches/binary_search.py:213:32: W291 Trailing whitespace help: Remove trailing whitespace
elif item < current_item:
right = midpoint - 1
else:
Expand Down
38 changes: 38 additions & 0 deletions tests/test_binary_search_issue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import sys

Check failure on line 1 in tests/test_binary_search_issue.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (INP001)

tests/test_binary_search_issue.py:1:1: INP001 File `tests/test_binary_search_issue.py` is part of an implicit namespace package. Add an `__init__.py`.
import os
from hypothesis import given, strategies as st

Check failure on line 3 in tests/test_binary_search_issue.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (I001)

tests/test_binary_search_issue.py:1:1: I001 Import block is un-sorted or un-formatted help: Organize imports

# Add root directory to python path
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from searches.binary_search import binary_search

# Strategy to generate a sorted list of integers
sorted_list_strategy = st.lists(st.integers(), min_size=0).map(sorted)

@given(
arr=sorted_list_strategy,
# Pick a random integer as a potential target
target=st.integers()

Check failure on line 16 in tests/test_binary_search_issue.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (W291)

tests/test_binary_search_issue.py:16:25: W291 Trailing whitespace help: Remove trailing whitespace
)
def test_binary_search_property_based(arr, target):
"""
Property-based test:
1. If target is in arr, binary_search MUST return an index 'i'

Check failure on line 21 in tests/test_binary_search_issue.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (W291)

tests/test_binary_search_issue.py:21:67: W291 Trailing whitespace help: Remove trailing whitespace
such that arr[i] == target.
2. If target is in arr, it MUST be the FIRST occurrence.
3. If target is not in arr, binary_search MUST return -1.
"""

Check failure on line 26 in tests/test_binary_search_issue.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (W293)

tests/test_binary_search_issue.py:26:1: W293 Blank line contains whitespace help: Remove whitespace from blank line
result = binary_search(arr, target)

Check failure on line 28 in tests/test_binary_search_issue.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (W293)

tests/test_binary_search_issue.py:28:1: W293 Blank line contains whitespace help: Remove whitespace from blank line
if target in arr:
assert result != -1, f"Target {target} was in {arr} but not found."
assert arr[result] == target, f"Index {result} pointed to {arr[result]}, not {target}."

# Property: Verify it is the FIRST occurrence
# Only check if the target is actually at the result index
for i in range(result):
assert arr[i] != target, f"Found target {target} at index {result}, but earlier occurrence at {i}."
else:
assert result == -1, f"Target {target} was not in {arr}, but found at index {result}."
Loading
Loading