human
Check if all rows of a Binary Matrix have all ones placed adjacently or not | Function to check if all 1 s are placed adjacently in an array or not.
Base Case.
Stores the sum of XOR of all pair of adjacent elements.
Calculate sum of XOR of all pair of adjacent elements.
Check for corner cases.
Return true.
Function to check if all the rows have all 1 s grouped together or not.
Traverse each row.
Check if all 1 s are placed together in the ith row or not.
Function to check if all 1 s in a row are grouped together in a matrix or not.
Print the result.
Given matrix.
Function Call"
How can the above be solved in Python?
 ... Can you provide a code to check if all rows of a Binary Matrix have all ones placed adjacently or not and the other steps required?
assistant
#Here is code to check if all rows of a Binary Matrix have all ones placed adjacently or not and the other steps required.
 def checkGroup ( arr ) :
	if len ( arr ) <= 2 :
		return True
	corner = arr [ 0 ] + arr [ - 1 ]
	xorSum = 0
	for i in range ( len ( arr ) - 1 ) :
		xorSum += ( arr [ i ] ^ arr [ i + 1 ] )
	if not corner :
		if xorSum > 2 :
			return False
	elif corner == 1 :
		if xorSum > 1 :
			return False
	else :
		if xorSum > 0 :
			return False
	return True
def isInGroupUtil ( mat ) :
	for i in mat :
		if not checkGroup ( i ) :
			return False
	return True
def isInGroup ( mat ) :
	ans = isInGroupUtil ( mat )
	if ans :
		print ( " Yes " )
	else :
		print ( " No " )
mat = [ [ 0 , 1 , 1 , 0 ] , [ 1 , 1 , 0 , 0 ] , [ 0 , 0 , 0 , 1 ] , [ 1 , 1 , 1 , 0 ] ]
isInGroup ( mat )