Skip to content

Instantly share code, notes, and snippets.

@HeySreelal
Created February 20, 2024 06:27
Show Gist options
  • Save HeySreelal/41710edc99528b0dc47ccb4a10a20b45 to your computer and use it in GitHub Desktop.
Save HeySreelal/41710edc99528b0dc47ccb4a10a20b45 to your computer and use it in GitHub Desktop.
Model lab examination shits :)
def binary_search(arr, target):
left = 0
right = 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
lower, upper = map(int, input("Enter lower and upper bound: ").split(" "))
arr = list(map(int, input("Enter numbers: ").split(" ")))
# Modification
# Filter out numbers that are not in the range
l = [x for x in arr if x >= lower and x <= upper]
target = int(input("Enter number to search: "))
result = binary_search(l, target)
if result != -1:
print(f"Element found at index {result}")
else:
print("Element not found")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment