From 8dab6c269591a978c8c037c1c56162d0ec3cd8ed Mon Sep 17 00:00:00 2001 From: Aayush Thapaliya <106351327+aayush6200@users.noreply.github.com> Date: Sat, 1 Jul 2023 01:17:32 -0700 Subject: [PATCH] added matrix --- algorithms/Python/matrix/rotate_matrix.py | 36 +++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 algorithms/Python/matrix/rotate_matrix.py diff --git a/algorithms/Python/matrix/rotate_matrix.py b/algorithms/Python/matrix/rotate_matrix.py new file mode 100644 index 00000000..1cd8dbdf --- /dev/null +++ b/algorithms/Python/matrix/rotate_matrix.py @@ -0,0 +1,36 @@ +## the code below explains the way to rotate matrix/image by 90 degrees + +# let matrix=[[1,2,3],[4,5,6],[7,8,9]] + +# matrix_after_rotation=[[7,4,1],[8,5,2],[9,6,3]] + +## algorithm: +# -> swap rows using two pointers and then swap the index as a[i][j],a[j][i]=a[j][i],a[i][j] + +def matrix_rotation_90_degree(matrix): + ## using two pointer + l,r=0,len(matrix)-1 + + while l < r: + matrix[l],matrix[r] = matrix[r],matrix[l] ## swapping rows using two pointers + l+=1 + r-=1 + # result + # # matrix=[[7,8,9],[4,5,6],[1,2,3]] + + ## swapping the rows and cols of every element of matrix + + for i in range(len(matrix)): + for j in range(i,len(matrix)): + ## at every iteration the row is swap with the column anc vice versa + matrix[i][j],matrix[j][i]=matrix[j][i],matrix[i][j] + + # result + # matrix=[[7,4,1],[8,5,2],[9,6,3]] + return matrix + + +# prints([[7,4,1],[8,5,2],[9,6,3]]) +print(matrix_rotation_90_degree([[1,2,3],[4,5,6],[7,8,9]])) + +