Created
February 20, 2024 06:27
-
-
Save HeySreelal/41710edc99528b0dc47ccb4a10a20b45 to your computer and use it in GitHub Desktop.
Model lab examination shits :)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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