From f39b7dffe072072c4ed9b63e82b99e54b966ebf8 Mon Sep 17 00:00:00 2001 From: sahusoham2020-ai Date: Fri, 31 Oct 2025 18:51:54 +0530 Subject: [PATCH] Add matrix rotation and display functions Implement functions to rotate and display a matrix. --- ROTATE.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 ROTATE.py diff --git a/ROTATE.py b/ROTATE.py new file mode 100644 index 0000000..c96555c --- /dev/null +++ b/ROTATE.py @@ -0,0 +1,34 @@ +# Function to rotate a N x M matrix by 90 degrees in clockwise direction +def rotateMatrix(mat): + n = len(mat) + m = len(mat[0]) + new_mat = [[0] * n for _ in range(m)] + for i in range(n): + for j in range(m): + new_mat[j][n - i - 1] = mat[i][j] + return new_mat + +# Function to print the matrix +def displayMatrix(mat): + n = len(mat) + m = len(mat[0]) + for i in range(n): + for j in range(m): + print(mat[i][j], end=" ") + print() + print() + +# Driver code +if __name__ == "__main__": + # Test Case 1 + mat = [ + [1, 2, 3, 4], + [5, 6, 7, 8], + [9, 10, 11, 12], + ] + + # Function call to rotate matrix + mat = rotateMatrix(mat) + + # Print rotated matrix + displayMatrix(mat)