I'm struggling to understand where the error is in this code:
def arr_sort_binsearch(ar):
ar.sort()
first = 0
last = len(ar)-1
found = False
item = 8
for num in ar:
while first<=last and not found:
midpoint = (first + last)//2
if ar[midpoint] + num == item:
found = True
else:
if item < ar[midpoint]:
last = midpoint-1
else:
first = midpoint+1
print("{} and {} is: {}".format(num, ar[num], item))
ar = [1,3,5,7]
arr_sort_binsearch(ar)
I'm getting an exception that says my index is out of range. I understand the theory of what an index overflow is it's just that I can't find it in my code.
numtoar[midpoint]? Wouldn'tar[midpoint]be the value you'd like to test againstitem?