Python Online Compiler
Example: Rotate Matrix 90 degrees Clockwise Python Program
C
C++
C#
Java
Python
PHP
main.py
STDIN
Run
# Rotate Matrix 90 degrees Clockwise Python Program # It's the function to rotate # the matrix 90 degree clock-wise def rotate90DegClockwise(arru, N): pass # It will traverse the each cycle for i in range (int (N / 2)): for j in range (i, int (N - i - 1)): # It will swap elements of each cycle in clock-wise direction temp = arru[i][j] arru[i][j] = arru[N - 1 - j][i] arru[N - 1 - j][i] = arru[N - 1 - i][N - 1 - j] arru[N - 1 - i][N - 1 - j] = arru[j][N - 1 - i] arru[j][N - 1 - i] = temp # It's the function for printing the rotated matrix def printMatrix(arru, N): pass for i in range (N): print (end="\t") for j in range (N): print (arru[i][j], end="\t") print (end="\n\n") # This is the driven calculation of matrix rotation arru = [ [13, 13, 17, 11], [25, 26, 27, 28], [39, 30, 31, 31], [43, 44, 45, 46] ] # This will print the matrix before rotation print ("-----The input matrix before 90 degrees clock-wise rotation-----\n") printMatrix(arru, 4) # This function will be rotate each element clock-wise rotate90DegClockwise(arru, 4) # This will print the matrix after rotation print ("\n-----The matrix after 90 degrees clock-wise rotation-----\n") printMatrix(arru, 4)
Output
Clear
ADVERTISEMENTS