Skip to content

Commit d6b27d8

Browse files
Merge pull request #34 from miguelHx/miguel_1.7_rotate_matrix
Miguel 1.7 - Rotate Matrix [Python]
2 parents 1803cea + 036a3c3 commit d6b27d8

File tree

1 file changed

+185
-0
lines changed

1 file changed

+185
-0
lines changed
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
"""
2+
Python version 3.7.0
3+
1.7 - Rotate Matrix
4+
Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a method to rotate
5+
the image by 90 degrees. Can you do this in place?
6+
"""
7+
import unittest
8+
from typing import List
9+
10+
11+
def rotate_matrix(matrix: List[List[int]]) -> List[List[int]]:
12+
"""
13+
Rotate matrix will rotate the given matrix by 90 degrees.
14+
Runtime: O(N^2), asymptotic runtime depends on N
15+
Space Complexity: O(N^2), creating a new matrix of NxN called 'rotated'
16+
:param matrix: an NxN matrix
17+
:param N: the size of the matrix (NxN)
18+
:return: a newly rotated matrix
19+
"""
20+
N = len(matrix)
21+
rotated = [[0 for i in range(N)] for j in range(N)]
22+
for i in range(N):
23+
for j in range(N):
24+
rotated[j][(N-1)-i] = matrix[i][j]
25+
return rotated
26+
27+
28+
def _perform_full_rotation(matrix: List[List[int]], row: int, col: int, M: int):
29+
"""
30+
Helper function that performs four 90 degree rotations starting at row, col.
31+
at a particular row and column from the matrix, there will be 4 rotations.
32+
visually, the algorithm looks like this:
33+
[start] (4th swap with original 3rd) - - - - - - - - - - - - - - - - - - -> (1st gets swapped with start)
34+
| |
35+
| |
36+
| |
37+
| |
38+
(3rd swap with original 2nd) < - - - - - - - - - - - - - - - - - - - - - - (2nd gets swapped with original 1st)
39+
this results in a full rotation in-place of a particular row and col coordinate.
40+
:param matrix: an MxM sub-matrix out of an NxN matrix
41+
:param row: starting row
42+
:param col: starting column
43+
:param M: size of sub-matrix
44+
:return:
45+
"""
46+
num_rotations = 4
47+
start_row = row
48+
rotated_row = row
49+
rotated_col = col
50+
temp_new = matrix[row][col]
51+
for _ in range(0, num_rotations):
52+
# compute new rotated indices
53+
# (start_row * 2) is an offset to account for reduced M into sub-matrices of matrix
54+
rotated_col, rotated_row = M - 1 - rotated_row + (start_row * 2), rotated_col
55+
# store value at newly computed indices
56+
temp_new, matrix[rotated_row][rotated_col] = matrix[rotated_row][rotated_col], temp_new
57+
58+
59+
def rotate_matrix_in_place(matrix: List[List[int]]) -> List[List[int]]:
60+
"""
61+
Does the same as rotate_matrix, but in place.
62+
Runtime: O(N^2), asymptotic runtime depends on N. We make N^2 swaps.
63+
Space Complexity: O(1), constant amount of temp variables that does not depend on N.
64+
:param matrix: an NxN matrix
65+
:return: the input matrix, but rotated
66+
"""
67+
N = len(matrix)
68+
# outer loop will keep reducing the matrix size 'n' by 2.
69+
# For example, a 6x6 matrix contains a 4x4 sub-matrix, we reduce
70+
# because at first, the values on the perimeter of the matrix will
71+
# be rotated after the inner for-loop, and we want to start rotating
72+
# the next sub-matrix perimeter.
73+
for start_row, n in enumerate(range(N, 1, -2)):
74+
# rotate along the current row's columns of the current sub-matrix perimeter
75+
for col in range(start_row, n - 1 + start_row):
76+
# performs 4 rotations for the current sub-matrix coordinate
77+
_perform_full_rotation(matrix, start_row, col, n)
78+
return matrix
79+
80+
81+
class TestRotateMatrixFunction(unittest.TestCase):
82+
83+
def setUp(self):
84+
self.cases = [
85+
(
86+
[
87+
[1, 2, 3, 4],
88+
[5, 6, 7, 8],
89+
[9, 10, 11, 12],
90+
[13, 14, 15, 16]
91+
],
92+
[
93+
[13, 9, 5, 1],
94+
[14, 10, 6, 2],
95+
[15, 11, 7, 3],
96+
[16, 12, 8, 4]
97+
]
98+
),
99+
(
100+
[
101+
[1, 2, 3],
102+
[4, 5, 6],
103+
[7, 8, 9]
104+
],
105+
[
106+
[7, 4, 1],
107+
[8, 5, 2],
108+
[9, 6, 3]
109+
]
110+
),
111+
(
112+
[
113+
[1]
114+
],
115+
[
116+
[1]
117+
]
118+
),
119+
(
120+
[
121+
[1, 2, 3, 4, 5, 6],
122+
[7, 8, 9, 10, 11, 12],
123+
[13, 14, 15, 16, 17, 18],
124+
[19, 20, 21, 22, 23, 24],
125+
[25, 26, 27, 28, 29, 30],
126+
[31, 32, 33, 34, 35, 36]
127+
],
128+
[
129+
[31, 25, 19, 13, 7, 1],
130+
[32, 26, 20, 14, 8, 2],
131+
[33, 27, 21, 15, 9, 3],
132+
[34, 28, 22, 16, 10, 4],
133+
[35, 29, 23, 17, 11, 5],
134+
[36, 30, 24, 18, 12, 6]
135+
]
136+
),
137+
(
138+
[
139+
[1, 2, 3, 4, 5],
140+
[6, 7, 8, 9, 10],
141+
[11, 12, 13, 14, 15],
142+
[16, 17, 18, 19, 20],
143+
[21, 22, 23, 24, 25]
144+
],
145+
[
146+
[21, 16, 11, 6, 1],
147+
[22, 17, 12, 7, 2],
148+
[23, 18, 13, 8, 3],
149+
[24, 19, 14, 9, 4],
150+
[25, 20, 15, 10, 5]
151+
]
152+
),
153+
(
154+
[
155+
[1, 2, 3, 4, 5, 6, 7],
156+
[8, 9, 10, 11, 12, 13, 14],
157+
[15, 16, 17, 18, 19, 20, 21],
158+
[22, 23, 24, 25, 26, 27, 28],
159+
[29, 30, 31, 32, 33, 34, 35],
160+
[36, 37, 38, 39, 40, 41, 42],
161+
[43, 44, 45, 46, 47, 48, 49]
162+
],
163+
[
164+
[43, 36, 29, 22, 15, 8, 1],
165+
[44, 37, 30, 23, 16, 9, 2],
166+
[45, 38, 31, 24, 17, 10, 3],
167+
[46, 39, 32, 25, 18, 11, 4],
168+
[47, 40, 33, 26, 19, 12, 5],
169+
[48, 41, 34, 27, 20, 13, 6],
170+
[49, 42, 35, 28, 21, 14, 7]
171+
]
172+
)
173+
]
174+
175+
def test_rotate_matrix(self):
176+
for matrix, expected in self.cases:
177+
self.assertEqual(rotate_matrix(matrix), expected, msg=(matrix, expected))
178+
179+
def test_rotate_matrix_in_place(self):
180+
for matrix, expected in self.cases:
181+
self.assertEqual(rotate_matrix_in_place(matrix), expected, msg=(matrix, expected))
182+
183+
184+
if __name__ == '__main__':
185+
unittest.main()

0 commit comments

Comments
 (0)