def binary_search(a, key):
low = 0
high = len(a) - 1
while low < high:
mid = (low + high) // 2
if key == a[mid]:
return True
elif key < mid:
high = mid - 1
else:
low = mid + 1
return False
"""Binary Search
Iterative
"""
def bin_iter(lst, item, low=0, high=None):
if high is None:
high = len(lst) - 1
while low <= high:
mid = (low + high) // 2
if item > lst[mid]: # To the left
low = mid + 1
elif item < lst[mid]: # To the right
high = mid - 1
else: # Found
return mid
return [] # Not found