Skip to content

Commit 3c0af47

Browse files
committed
Python solution for rotate matrix by 90 in anticlockwise
1 parent d0f2983 commit 3c0af47

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

DSA 450 GFG/rotate_by_90.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
3+
#https://practice.geeksforgeeks.org/problems/rotate-by-90-degree0356/1/?company[]=Morgan%20Stanley&company[]=Morgan%20Stanley&page=1&query=company[]Morgan%20Stanleypage1company[]Morgan%20Stanley
4+
5+
6+
def rotate(a):
7+
#code here
8+
9+
n = len(a)
10+
11+
# Transpose
12+
for i in range(n):
13+
for j in range(i):
14+
a[i][j] , a[j][i] = a[j][i] , a[i][j]
15+
16+
# Rotate the rows
17+
18+
for i in range(int(n/2)):
19+
a[i] , a[n-i-1] = a[n-i-1] , a[i]
20+
21+
return a
22+
23+
#{
24+
# Driver Code Starts
25+
#Initial Template for Python 3
26+
27+
28+
if __name__ == '__main__':
29+
t = int(input())
30+
for _ in range(t):
31+
N=int(input())
32+
arr=[int(x) for x in input().split()]
33+
matrix=[]
34+
35+
for i in range(0,N*N,N):
36+
matrix.append(arr[i:i+N])
37+
38+
rotate(matrix)
39+
for i in range(N):
40+
for j in range(N):
41+
print(matrix[i][j], end =' ')
42+
print()
43+
44+
45+
# } Driver Code Ends

0 commit comments

Comments
 (0)