Swap the elements between any two given quadrants of a Matrix | Python3 program for the above approach ; Function to iterate over the X quadrant and swap its element with Y quadrant ; Iterate over X quadrant ; Swap operations ; Function to swap the elements of the two given quadrants ; For Swapping 1 st and 2 nd Quadrant ; For Swapping 1 st and 3 rd Quadrant ; For Swapping 1 st and 4 th Quadrant ; For Swapping 2 nd and 3 rd Quadrant ; For Swapping 2 nd and 4 th Quadrant ; For Swapping 3 rd and 4 th Quadrant ; Print the resultant matrix ; Function to print the matrix ; Iterate over the rows ; Iterate over the cols ; Given matrix ; Given quadrants ; Function Call
N , M = 6 , 6 NEW_LINE def swap ( mat , startx_X , starty_X , startx_Y , starty_Y ) : NEW_LINE INDENT row , col = 0 , 0 NEW_LINE i = startx_X NEW_LINE while ( bool ( True ) ) : NEW_LINE INDENT col = 0 NEW_LINE j = startx_X NEW_LINE while ( bool ( True ) ) : NEW_LINE INDENT temp = mat [ i ] [ j ] NEW_LINE mat [ i ] [ j ] = mat [ startx_Y + row ] [ starty_Y + col ] NEW_LINE mat [ startx_Y + row ] [ starty_Y + col ] = temp NEW_LINE col += 1 NEW_LINE if col >= M // 2 : NEW_LINE INDENT break NEW_LINE DEDENT j += 1 NEW_LINE DEDENT row += 1 NEW_LINE if row >= N // 2 : NEW_LINE INDENT break NEW_LINE DEDENT i += 1 NEW_LINE DEDENT DEDENT def swapQuadOfMatrix ( mat , X , Y ) : NEW_LINE INDENT if ( X == 1 and Y == 2 ) : NEW_LINE INDENT swap ( mat , 0 , 0 , 0 , M // 2 ) NEW_LINE DEDENT elif ( X == 1 and Y == 3 ) : NEW_LINE INDENT swap ( mat , 0 , 0 , N // 2 , 0 ) NEW_LINE DEDENT elif ( X == 1 and Y == 4 ) : NEW_LINE INDENT swap ( mat , 0 , 0 , N // 2 , M // 2 ) NEW_LINE DEDENT elif ( X == 2 and Y == 3 ) : NEW_LINE INDENT swap ( mat , 0 , M // 2 , N // 2 , 0 ) NEW_LINE DEDENT elif ( X == 2 and Y == 4 ) : NEW_LINE INDENT swap ( mat , 0 , M // 2 , N // 2 , M // 2 ) NEW_LINE DEDENT elif ( X == 3 and Y == 4 ) : NEW_LINE INDENT swap ( mat , N // 2 , 0 , N // 2 , M // 2 ) NEW_LINE DEDENT printMat ( mat ) NEW_LINE DEDENT def printMat ( mat ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT for j in range ( M ) : 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 , 17 , 18 ] , [ 19 , 20 , 21 , 22 , 23 , 24 ] , [ 25 , 26 , 27 , 28 , 29 , 30 ] , [ 31 , 32 , 33 , 34 , 35 , 36 ] ] NEW_LINE X , Y = 1 , 4 NEW_LINE swapQuadOfMatrix ( mat , X , Y ) NEW_LINE