From e9fe514653ac402cbd61df41ec00d0b437af1ac8 Mon Sep 17 00:00:00 2001 From: Mohit yadav Date: Sat, 6 Oct 2018 02:25:12 +0530 Subject: [PATCH] Rotaten*n matrix inplace --- rotatenbyn_inplace.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 rotatenbyn_inplace.py diff --git a/rotatenbyn_inplace.py b/rotatenbyn_inplace.py new file mode 100644 index 0000000..d730ee0 --- /dev/null +++ b/rotatenbyn_inplace.py @@ -0,0 +1,43 @@ +''' +Question Statement +Rotate a n*n matrix by 90 degrees without using extra space + +''' +N = 4 + +def rotateMatrix(matrix): + + for i in range(0, int(N/2)): + + for j in range(i, N-i-1): + + tmp = matrix[i][j] + + matrix[i][j] = matrix[j][N-1-i] + + matrix[j][N-1-i] = matrix[N-1-i][N-1-j] + + matrix[N-1-i][N-1-j] = matrix[N-1-j][i] + + matrix[N-1-j][i] = tmp + + +def displayMatrix( matrix ): + + for i in range(0, N): + + for j in range(0, N): + + print (matrix[i][j]) + print ("") + + + +matrix = [[0 for i in range(N)] for j in range(N)] +matrix = [ [0, 1, 2, 3 ], + [00, 11, 22, 33 ], + [44, 55, 66, 77 ], + [88, 99, 110, 120 ] ] +rotateMatrix(matrix) + +displayMatrix(matrix) \ No newline at end of file