Mirror of matrix across diagonal | Simple Python3 program to find mirror of matrix across diagonal . ; for diagonal which start from at first row of matrix ; traverse all top right diagonal ; here we use stack for reversing the element of diagonal ; push all element back to matrix in reverse order ; do the same process for all the diagonal which start from last column ; here we use stack for reversing the elements of diagonal ; push all element back to matrix in reverse order ; Utility function to pra matrix ; Driver code
MAX = 100 NEW_LINE def imageSwap ( mat , n ) : NEW_LINE INDENT row = 0 NEW_LINE for j in range ( n ) : NEW_LINE INDENT s = [ ] NEW_LINE i = row NEW_LINE k = j NEW_LINE while ( i < n and k >= 0 ) : NEW_LINE INDENT s . append ( mat [ i ] [ k ] ) NEW_LINE i += 1 NEW_LINE k -= 1 NEW_LINE DEDENT i = row NEW_LINE k = j NEW_LINE while ( i < n and k >= 0 ) : NEW_LINE INDENT mat [ i ] [ k ] = s [ - 1 ] NEW_LINE k -= 1 NEW_LINE i += 1 NEW_LINE s . pop ( ) NEW_LINE DEDENT DEDENT column = n - 1 NEW_LINE for j in range ( 1 , n ) : NEW_LINE INDENT s = [ ] NEW_LINE i = j NEW_LINE k = column NEW_LINE while ( i < n and k >= 0 ) : NEW_LINE INDENT s . append ( mat [ i ] [ k ] ) NEW_LINE i += 1 NEW_LINE k -= 1 NEW_LINE DEDENT i = j NEW_LINE k = column NEW_LINE while ( i < n and k >= 0 ) : NEW_LINE INDENT mat [ i ] [ k ] = s [ - 1 ] NEW_LINE i += 1 NEW_LINE k -= 1 NEW_LINE s . pop ( ) NEW_LINE DEDENT DEDENT DEDENT def printMatrix ( mat , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT print ( mat [ i ] [ j ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT mat = [ [ 1 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 8 ] , [ 9 , 10 , 11 , 12 ] , [ 13 , 14 , 15 , 16 ] ] NEW_LINE n = 4 NEW_LINE imageSwap ( mat , n ) NEW_LINE printMatrix ( mat , n ) NEW_LINE