I have the following code which gets two arrays and finds the location of each item in the second array based on the first array. For example, for 23 from loc1
, which is between 20 and 25 in array it should return 20.
matrix_x = []
def binarySearch(alist, loc):
for item in loc:
midpoint = len(alist)//2
if midpoint == 1:
if item<alist[midpoint]:
return matrix_x.append(alist[midpoint-1])
else:
return matrix_x.append(alist[midpoint])
else:
if item<alist[midpoint]:
return binarySearch(alist[:midpoint],loc)
else:
return binarySearch(alist[midpoint:],loc)
return matrix_x
array = [5,10,15,20,25]
loc1= [23,7,11]
print(binarySearch(array, loc1))
print(matrix_x)
I expect to receive this array as the result:
[20,5,10]
But I receive only the first item like this:
[20]