human
Write me a code to maximum splits in binary string such that each substring is divisible by given odd number and the other steps required given the following:
Maximum splits in binary string such that each substring is divisible by given odd number | Function to calculate maximum splits possible.
Driver code"
Solution in Python:
assistant
#Here is code to maximum splits in binary string such that each substring is divisible by given odd number and the other steps required.
 def max_segments ( st , K ) :
	n = len ( st )
	s , sum , count = 0 , 0 , 0
	for i in range ( n - 1 , - 1 , - 1 ) :
		a = ord ( st [ i ] ) - 48
		sum += a * pow ( 2 , s )
		s += 1
		if ( sum != 0 and sum % K == 0 ) :
			count += 1
			sum = 0
			s = 0
	if ( sum != 0 ) :
		print ( " - 1" )
	else :
		print ( count )
if __name__ == " _ _ main _ _ " :
	st = "10111001"
	K = 5
	max_segments ( st , K )