1
Hello, I want to create a function that given a list to and a value m within this list, return the position of the value m. My idea was to start with the extreme values of the ordered list and take the midpoint until you find the value m (and then return its position):
def bissecp(a,m):
n = len(a) - 1
i=0
encontrou = False
meio = int(((a[i] + a[n]) / 2))
if m==a[i]:
return i
if m==a[n]:
return n
index = 0
if m == meio:
encontrou == True
return int((i+n)/2)
while encontrou == False:
if m> meio:
i+=1
meio = int((a[i] + a[n])/2)
if m == meio:
index = int((i+n)/2)
encontrou==True
continue
if m < meio:
i += 1
meio = int((a[i] + a[n-i]) / 2)
if m == meio:
index = int((a[i] + a[n-i])/2)
encontrou==True
return index
a = list(range(1, 100))
but is returning the following error :
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "<input>", line 28, in bissecp
IndexError: list index out of range