5
5
the image by 90 degrees. Can you do this in place?
6
6
"""
7
7
import unittest
8
- from typing import List
8
+ from typing import List , Optional
9
9
10
10
11
- def rotate_matrix (matrix : List [List [int ]], N : int ) -> List [List [int ]]:
11
+ def rotate_matrix (matrix : List [List [int ]]) -> List [List [int ]]:
12
12
"""
13
13
Rotate matrix will rotate the given matrix by 90 degrees.
14
14
Runtime: O(N^2), asymptotic runtime depends on N
@@ -17,14 +17,19 @@ def rotate_matrix(matrix: List[List[int]], N: int) -> List[List[int]]:
17
17
:param N: the size of the matrix (NxN)
18
18
:return: a newly rotated matrix
19
19
"""
20
+ N = len (matrix )
20
21
rotated = [[0 for i in range (N )] for j in range (N )]
21
22
for i in range (N ):
22
23
for j in range (N ):
23
24
rotated [j ][(N - 1 )- i ] = matrix [i ][j ]
24
25
return rotated
25
26
26
27
27
- def rotate_matrix_in_place (matrix : List [List [int ]], start_row : int , start_col : int , N : int ) -> List [List [int ]]:
28
+ def rotate_matrix_in_place (
29
+ matrix : List [List [int ]],
30
+ start_row : int = 0 ,
31
+ start_col : int = 0 ,
32
+ N : Optional [int ] = None ) -> List [List [int ]]:
28
33
"""
29
34
Does the same as rotate_matrix, but in place.
30
35
Runtime: O(N^2), asymptotic runtime depends on N. We make N^2 swaps.
@@ -36,6 +41,9 @@ def rotate_matrix_in_place(matrix: List[List[int]], start_row: int, start_col: i
36
41
:param N: the size of the matrix (NxN)
37
42
:return: the input matrix, but rotated
38
43
"""
44
+
45
+ if N is None :
46
+ N = len (matrix )
39
47
num_rotations = 4
40
48
if N == 0 or N == 1 :
41
49
return matrix
@@ -70,7 +78,6 @@ def setUp(self):
70
78
[9 , 10 , 11 , 12 ],
71
79
[13 , 14 , 15 , 16 ]
72
80
],
73
- 4 ,
74
81
[
75
82
[13 , 9 , 5 , 1 ],
76
83
[14 , 10 , 6 , 2 ],
@@ -84,7 +91,6 @@ def setUp(self):
84
91
[4 , 5 , 6 ],
85
92
[7 , 8 , 9 ]
86
93
],
87
- 3 ,
88
94
[
89
95
[7 , 4 , 1 ],
90
96
[8 , 5 , 2 ],
@@ -95,7 +101,6 @@ def setUp(self):
95
101
[
96
102
[1 ]
97
103
],
98
- 1 ,
99
104
[
100
105
[1 ]
101
106
]
@@ -109,7 +114,6 @@ def setUp(self):
109
114
[25 , 26 , 27 , 28 , 29 , 30 ],
110
115
[31 , 32 , 33 , 34 , 35 , 36 ]
111
116
],
112
- 6 ,
113
117
[
114
118
[31 , 25 , 19 , 13 , 7 , 1 ],
115
119
[32 , 26 , 20 , 14 , 8 , 2 ],
@@ -127,7 +131,6 @@ def setUp(self):
127
131
[16 , 17 , 18 , 19 , 20 ],
128
132
[21 , 22 , 23 , 24 , 25 ]
129
133
],
130
- 5 ,
131
134
[
132
135
[21 , 16 , 11 , 6 , 1 ],
133
136
[22 , 17 , 12 , 7 , 2 ],
@@ -146,7 +149,6 @@ def setUp(self):
146
149
[36 , 37 , 38 , 39 , 40 , 41 , 42 ],
147
150
[43 , 44 , 45 , 46 , 47 , 48 , 49 ]
148
151
],
149
- 7 ,
150
152
[
151
153
[43 , 36 , 29 , 22 , 15 , 8 , 1 ],
152
154
[44 , 37 , 30 , 23 , 16 , 9 , 2 ],
@@ -160,14 +162,12 @@ def setUp(self):
160
162
]
161
163
162
164
def test_rotate_matrix (self ):
163
- for matrix , N , expected in self .cases :
164
- self .assertEqual (rotate_matrix (matrix , N ), expected , msg = (matrix , N , expected ))
165
+ for matrix , expected in self .cases :
166
+ self .assertEqual (rotate_matrix (matrix ), expected , msg = (matrix , expected ))
165
167
166
168
def test_rotate_matrix_in_place (self ):
167
- start_row = 0
168
- start_col = 0
169
- for matrix , N , expected in self .cases :
170
- self .assertEqual (rotate_matrix_in_place (matrix , start_row , start_col , N ), expected , msg = (matrix , N , expected ))
169
+ for matrix , expected in self .cases :
170
+ self .assertEqual (rotate_matrix_in_place (matrix ), expected , msg = (matrix , expected ))
171
171
172
172
173
173
if __name__ == '__main__' :
0 commit comments