human
What is a code to count all sorted rows in a matrix and the other steps required given the following:
Count all sorted rows in a matrix | Function to count all sorted rows in a matrix.
Initialize result.
Start from left side of matrix to count increasing order rows.
Check if there is any pair ofs element that are not in increasing order ..
If the loop didn 't break (All elements of current row were in increasing order).
Start from right side of matrix to count increasing order rows ( reference to left these are in decreasing order ).
Check if there is any pair ofs element that are not in decreasing order ..
Note c > 1 condition is required to make sure that a single column row is not counted twice ( Note that a single column row is sorted both in increasing and decreasing order ).
Driver code"
Solution in Python:
assistant
#Here is code to count all sorted rows in a matrix and the other steps required.
 def sortedCount ( mat , r , c ) :
	result = 0
	for i in range ( r ) :
		j = 0
		for j in range ( c - 1 ) :
			if mat [ i ] [ j + 1 ] <= mat [ i ] [ j ] :
				break
		if j == c - 2 :
			result += 1
	for i in range ( 0 , r ) :
		j = 0
		for j in range ( c - 1 , 0 , - 1 ) :
			if mat [ i ] [ j - 1 ] <= mat [ i ] [ j ] :
				break
		if c > 1 and j == 1 :
			result += 1
	return result
m , n = 4 , 5
mat = [ [ 1 , 2 , 3 , 4 , 5 ] , [ 4 , 3 , 1 , 2 , 6 ] , [ 8 , 7 , 6 , 5 , 4 ] , [ 5 , 7 , 8 , 9 , 10 ] ]
print ( sortedCount ( mat , m , n ) )