Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created November 16, 2019 12:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codecademydev/0ddf4b63ae4a8c9f26912c432fa5245d to your computer and use it in GitHub Desktop.
Save codecademydev/0ddf4b63ae4a8c9f26912c432fa5245d to your computer and use it in GitHub Desktop.
Codecademy export
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