Skip to content

Commit 30c1516

Browse files
committed
remove N param and make some params optional in rotate_matrix_in_place
1 parent a66b6ad commit 30c1516

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

Python/chapter01/1.7 - Rotate Matrix/miguel_1.7_sol.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
the image by 90 degrees. Can you do this in place?
66
"""
77
import unittest
8-
from typing import List
8+
from typing import List, Optional
99

1010

11-
def rotate_matrix(matrix: List[List[int]], N: int) -> List[List[int]]:
11+
def rotate_matrix(matrix: List[List[int]]) -> List[List[int]]:
1212
"""
1313
Rotate matrix will rotate the given matrix by 90 degrees.
1414
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]]:
1717
:param N: the size of the matrix (NxN)
1818
:return: a newly rotated matrix
1919
"""
20+
N = len(matrix)
2021
rotated = [[0 for i in range(N)] for j in range(N)]
2122
for i in range(N):
2223
for j in range(N):
2324
rotated[j][(N-1)-i] = matrix[i][j]
2425
return rotated
2526

2627

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]]:
2833
"""
2934
Does the same as rotate_matrix, but in place.
3035
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
3641
:param N: the size of the matrix (NxN)
3742
:return: the input matrix, but rotated
3843
"""
44+
45+
if N is None:
46+
N = len(matrix)
3947
num_rotations = 4
4048
if N == 0 or N == 1:
4149
return matrix
@@ -70,7 +78,6 @@ def setUp(self):
7078
[9, 10, 11, 12],
7179
[13, 14, 15, 16]
7280
],
73-
4,
7481
[
7582
[13, 9, 5, 1],
7683
[14, 10, 6, 2],
@@ -84,7 +91,6 @@ def setUp(self):
8491
[4, 5, 6],
8592
[7, 8, 9]
8693
],
87-
3,
8894
[
8995
[7, 4, 1],
9096
[8, 5, 2],
@@ -95,7 +101,6 @@ def setUp(self):
95101
[
96102
[1]
97103
],
98-
1,
99104
[
100105
[1]
101106
]
@@ -109,7 +114,6 @@ def setUp(self):
109114
[25, 26, 27, 28, 29, 30],
110115
[31, 32, 33, 34, 35, 36]
111116
],
112-
6,
113117
[
114118
[31, 25, 19, 13, 7, 1],
115119
[32, 26, 20, 14, 8, 2],
@@ -127,7 +131,6 @@ def setUp(self):
127131
[16, 17, 18, 19, 20],
128132
[21, 22, 23, 24, 25]
129133
],
130-
5,
131134
[
132135
[21, 16, 11, 6, 1],
133136
[22, 17, 12, 7, 2],
@@ -146,7 +149,6 @@ def setUp(self):
146149
[36, 37, 38, 39, 40, 41, 42],
147150
[43, 44, 45, 46, 47, 48, 49]
148151
],
149-
7,
150152
[
151153
[43, 36, 29, 22, 15, 8, 1],
152154
[44, 37, 30, 23, 16, 9, 2],
@@ -160,14 +162,12 @@ def setUp(self):
160162
]
161163

162164
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))
165167

166168
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))
171171

172172

173173
if __name__ == '__main__':

0 commit comments

Comments
 (0)