forked from altruistcoder/Data-Structures-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
90_rotate_square_matrix_clockwise.py
41 lines (30 loc) · 1.06 KB
/
90_rotate_square_matrix_clockwise.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
def rotateMatrix(a, N):
# Consider all matrix cells one by one
for x in range(0, int(N/2)):
# Consider the elements in groups of 4 in the current square
for y in range(x, N-x-1):
# Store the value of current cell in the 'temp' variable
temp = a[x][y]
# Rotation of values from Left to Top
a[x][y] = a[N-1-y][x]
# Rotation of values from Bottom to Left
a[N-1-y][x] = a[N-1-x][N-1-y]
# Rotation of values from Right to Bottom
a[N-1-x][N-1-y] = a[y][N-1-x]
# Assign value of 'temp' to Right
a[y][N - 1 - x] = temp
def displayMatrix(a, N):
for i in range(0, N):
for j in range(0, N):
print(a[i][j], end=' ')
print("")
n = int(input("Enter the order of the matrix: "))
a = []
print("Enter the elements of the matrix: ")
for i in range(n):
a.append(list(map(int, input().split())))
print("Original Array: ")
displayMatrix(a, n)
print("90 Degrees Rotated Array:")
rotateMatrix(a, n)
displayMatrix(a, n)