human
Find an equal point in a string of brackets | Method to find an equal index.
Store the number of opening brackets at each index.
Store the number of closing brackets at each index.
check if there is no opening or closing brackets.
check if there is any index at which both brackets are equal.
Driver Code"
How can the above be solved in Python?
assistant
#Here is code to find an equal point in a string of brackets and the other steps required.
 def findIndex ( str ) :
	l = len ( str )
	open = [ 0 ] * ( l + 1 )
	close = [ 0 ] * ( l + 1 )
	index = - 1
	open [ 0 ] = 0
	close [ l ] = 0
	if ( str [ 0 ] == ' ( ' ) :
		open [ 1 ] = 1
	if ( str [ l - 1 ] == ' ) ' ) :
		close [ l - 1 ] = 1
	for i in range ( 1 , l ) :
		if ( str [ i ] == ' ( ' ) :
			open [ i + 1 ] = open [ i ] + 1
		else :
			open [ i + 1 ] = open [ i ]
	for i in range ( l - 2 , - 1 , - 1 ) :
		if ( str [ i ] == ' ) ' ) :
			close [ i ] = close [ i + 1 ] + 1
		else :
			close [ i ] = close [ i + 1 ]
	if ( open [ l ] == 0 ) :
		return len
	if ( close [ 0 ] == 0 ) :
		return 0
	for i in range ( l + 1 ) :
		if ( open [ i ] == close [ i ] ) :
			index = i
	return index
str = " ( ( ) ) ) ( ( ) ( ) ( ) ) ) ) "
print ( findIndex ( str ) )