human
Binary Search a String | Returns index of x if it is present in arr [ ] , else return - 1.
Check if x is present at mid.
If x greater , ignore left half.
If x is smaller , ignore right half.
Driver Code"
How can the above be solved in Python?
assistant
#Here is code to binary Search a String and the other steps required.
 def binarySearch ( arr , x ) :
	l = 0
	r = len ( arr )
	while ( l <= r ) :
		m = l + ( ( r - l ) // 2 )
		res = ( x == arr [ m ] )
		if ( res == 0 ) :
			return m - 1
		if ( res > 0 ) :
			l = m + 1
		else :
			r = m - 1
	return - 1
if __name__ == " _ _ main _ _ " :
	arr = [ " contribute " , " geeks " , " ide " , " practice " ] ;
	x = " ide "
	result = binarySearch ( arr , x )
	if ( result == - 1 ) :
		print ( " Element ▁ not ▁ present " )
	else :
		print ( " Element ▁ found ▁ at ▁ index " , result )