Simple Binary Search Python


#1

class Solution:
# @param A : tuple of integers
# @param B : integer
# @return a list of integers
def searchRange(self, A, B):
top=0
top1=0
minn=-1
maxx=-1
bot=len(A)-1
bot1=len(A)-1
while(bot>=top):
mid=top + (bot-top)//2
if(A[mid]==B):
if(mid<minn or minn==-1):
minn=mid
maxx=minn
if(mid>0 and A[mid-1]==B):
bot=mid-1
else:
break
elif(B>A[mid]):
top=mid+1
elif(B<A[mid]):
bot=mid-1
while(bot1>=top1):
mid=top1 +(bot1-top1)//2
if(A[mid]==B):
if(mid>maxx):
maxx=mid
if(mid<bot1 and A[mid+1]==B):
top1=mid+1
else:
break
elif(B>A[mid]):
top1=mid+1
elif(B<A[mid]):
bot1=mid-1
l=[minn,maxx]
return l