human
What is a code to minimize count of adjacent row swaps to convert given Matrix to a Lower Triangular Matrix and the other steps required given the following:
Minimize count of adjacent row swaps to convert given Matrix to a Lower Triangular Matrix | Function to count the minimum number of adjacent swaps.
Stores the size of the given matrix.
Stores the count of zero at the end of each row.
Traverse the given matrix.
Count of 0 s at the end of the ith row.
Stores the count of swaps.
Traverse the cntZero array.
If count of zero in the i - th row < ( N - i - 1 ).
Stores the index of the row where count of zero > ( N - i - 1 ).
If no row found that satisfy the condition.
Swap the adjacent row.
Driver Code"
How can the above be solved in Python?
assistant
#Here is code to minimize count of adjacent row swaps to convert given Matrix to a Lower Triangular Matrix and the other steps required.
 def minAdjSwaps ( mat ) :
	N = len ( mat )
	cntZero = [ 0 ] * ( N )
	for i in range ( N ) :
		for j in range ( N - 1 , - 1 , - 1 ) :
			if mat [ i ] [ j ] != 0 :
				break
			cntZero [ i ] += 1
	cntSwaps = 0
	for i in range ( N ) :
		if ( cntZero [ i ] < ( N - i - 1 ) ) :
			First = i
			while ( First < N and cntZero [ First ] < ( N - i - 1 ) ) :
				First += 1
			if ( First == N ) :
				return - 1
			while ( First > i ) :
				cntZero [ First ] = cntZero [ First - 1 ]
				cntZero [ First - 1 ] = cntZero [ First ]
				First -= 1
				cntSwaps += 1
	return cntSwaps
if __name__ == ' _ _ main _ _ ' :
	mat = [ [ 0 , 0 , 2 ] , [ 3 , 1 , 0 ] , [ 4 , 0 , 0 ] ]
	print ( minAdjSwaps ( mat ) )