-
-
Save codecademydev/0ddf4b63ae4a8c9f26912c432fa5245d to your computer and use it in GitHub Desktop.
Codecademy export
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 sparse_search(data, search_val): | |
print("Data: " + str(data)) | |
print("Search Value: " + str(search_val)) | |
first = 0 | |
last = len(data)-1 | |
while first<=last: | |
mid = (first+last)//2 | |
if not data[mid]: | |
left = mid-1 | |
right = mid+1 | |
while True: | |
if left < first and right > last: | |
print("{0} is not in the dataset".format(search_val)) | |
return | |
elif right <= last and data[right]: | |
mid = right | |
break | |
elif left >= first and data[left]: | |
mid = left | |
break | |
else: | |
right = right+1 | |
left = left-1 | |
if data[mid] == search_val: | |
print("{0} found at position {1}".format(search_val, mid)) | |
return | |
elif search_val < data[mid]: | |
last= mid-1 | |
elif search_val > data[mid]: | |
first= mid+1 | |
print("{0} is not in the dataset".format(search_val)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment