-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixInplaceRotation.py
More file actions
95 lines (64 loc) · 1.3 KB
/
Copy pathMatrixInplaceRotation.py
File metadata and controls
95 lines (64 loc) · 1.3 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# def say_hello():
# print('Hello, World')
# for i in range(5):
# say_hello()
'''
Give N*N matrix
rotate in-place image by 90 (clockwise)
[[1,2,3]
[4,5,6]
[7,8,9]
]
rotateImage
[[7,4,1]
[8,5,2
[9,5,3]
]
'''
def anticlockWiseRotation(matrix):
row = len(matrix)
# print(row)
for x in range(0, int(row / 2)):
for y in range(x, row - x - 1):
temp = matrix[x][y]
# to top
matrix[x][y] = matrix[y][row - x - 1]
matrix[y][row - x - 1] = matrix[row - x - 1][row - y - 1]
# print("Past line 46")
# to bottom
matrix[row - x - 1][row - y - 1] = matrix[row - y - 1][x]
matrix[row - y - 1][x] = temp
def printMatrix(matrix):
row = len(matrix)
for i in range(row):
for j in range(row):
print(matrix[i][j], end="")
print("")
test_matrix = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
# 1 2 3
# 4 5 6
# 7 8 9
# 3 2 3
# 4 5 6
# 7 8 9
# 3 2 9
# 4 5 6
# 7 8 9
# 3 2 9
# 4 5 6
# 7 8 7
# 3 2 9
# 4 5 6
# 1 8 7
# 3 6 9
# 4 5 6
# 1 8 7
printMatrix(test_matrix)
print("")
anticlockWiseRotation(test_matrix)
print("")
#clockWiseRotation(test_matrix)
printMatrix(test_matrix)