Sorting boundary elements of a matrix | Python program for the above approach ; Appending border elements ; Sorting the list ; Printing first row with first N elements from A ; Printing N - 2 rows ; Print elements from last ; Print middle elements from original matrix ; Print elements from front ; Printing last row ; Dimensions of a Matrix ; Given Matrix ; Function Call
def printMatrix ( grid , m , n ) : NEW_LINE INDENT A = [ ] NEW_LINE for i in range ( m ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT if j == n - 1 or ( i == m - 1 ) or j == 0 or i == 0 : NEW_LINE INDENT A . append ( grid [ i ] [ j ] ) NEW_LINE DEDENT DEDENT DEDENT A . sort ( ) NEW_LINE print ( * A [ : n ] ) NEW_LINE for i in range ( m - 2 ) : NEW_LINE INDENT print ( A [ len ( A ) - i - 1 ] , end = " ▁ " ) NEW_LINE for j in range ( 1 , n - 1 ) : NEW_LINE INDENT print ( grid [ i + 1 ] [ j ] , end = " ▁ " ) NEW_LINE DEDENT print ( A [ n + i ] ) NEW_LINE DEDENT print ( * reversed ( A [ n + m - 2 : n + m - 2 + n ] ) ) NEW_LINE DEDENT m , n = 4 , 5 NEW_LINE grid = [ [ 1 , 2 , 3 , 4 , 0 ] , [ 1 , 1 , 1 , 1 , 2 ] , [ 1 , 2 , 2 , 2 , 4 ] , [ 1 , 9 , 3 , 1 , 7 ] ] NEW_LINE printMatrix ( grid , m , n ) NEW_LINE