Skip to content

Instantly share code, notes, and snippets.

@jesuscast
Last active January 2, 2017 01:44
Show Gist options
  • Save jesuscast/31726912fd581f0754a3702961221cbe to your computer and use it in GitHub Desktop.
Save jesuscast/31726912fd581f0754a3702961221cbe to your computer and use it in GitHub Desktop.
def binarySearch(alist, item):
first = 0
last = len(alist)-1
index = None
while first<=last and not index:
midpoint = (first + last)//2
if alist[midpoint] >= item:
index = midpoint
else:
if item < alist[midpoint]:
last = midpoint-1
else:
first = midpoint+1
return index
def searchDateRange(data, initialDate, finalDate):
i = binarySearch(data, initialDate)
if not i:
print "Could not find lower bound."
return None
newArray = data[i:len(data)]
j = binarySearch(newArray, finalDate)
if not j:
print "Could not find upper bound"
return None
return data[i:j]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment