Skip to content
Open
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
13 changes: 13 additions & 0 deletions buggy_binary_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
def binary_search(arr, target):
left, right = 0, len(arr) - 1 # Bug fixed: use len(arr) - 1
while left <= right:
mid = (left + right) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1

print(binary_search([1, 2, 3, 4, 5], 3))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Avoid top-level side effects; gate the demo under main.

Prevents output on import.

-print(binary_search([1, 2, 3, 4, 5], 3))
+if __name__ == "__main__":
+    print(binary_search([1, 2, 3, 4, 5], 3))
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
print(binary_search([1, 2, 3, 4, 5], 3))
if __name__ == "__main__":
print(binary_search([1, 2, 3, 4, 5], 3))
🤖 Prompt for AI Agents
In buggy_binary_search.py around line 13, the direct call
print(binary_search([1, 2, 3, 4, 5], 3)) causes a top-level side effect on
import; move this demo into a guarded main block by creating a small main() (or
similar) that performs the call and printing, then wrap the invocation with if
__name__ == "__main__": main() so importing the module no longer produces
output.