Skip to content

Commit 3fc4dc5

Browse files
committedFeb 19, 2019
add 1.7_rotate_matrix.py
1 parent 13d9323 commit 3fc4dc5

File tree

4 files changed

+74
-4
lines changed

4 files changed

+74
-4
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
'''Question:
2+
Given an image represented by an NxN matrix, where each pixel in the image is
3+
4 bytes, write a method to rotate the image by 90 degrees.
4+
5+
Can you do this in place?
6+
7+
clarifications:
8+
- assume rotate closewise
9+
- input: matrix
10+
- return: None
11+
'''
12+
13+
from math import ceil
14+
import copy
15+
import unittest
16+
from test_utils import UnitTestBase
17+
18+
# Solution:
19+
# Hint: draw a 4x4 matrix as an example
20+
# Time: O(N*N)
21+
# Space: O(1)
22+
# Hotspot: if N is odd, it will be wrong to use range(ceil(n/2)) to loop i.
23+
# The reason is it will end up rotating twice for some cells
24+
def rotate_matrix(matrix):
25+
n = len(matrix)
26+
for i in range(n//2):
27+
for j in range(ceil(n/2)):
28+
tmp = matrix[i][j]
29+
matrix[i][j] = matrix[n - 1 - j][i]
30+
matrix[n - 1 - j][i] = matrix[n - 1 - i][n - 1 - j]
31+
matrix[n - 1 - i][n - 1 - j] = matrix[j][n - 1 - i]
32+
matrix[j][n - 1 - i] = tmp
33+
34+
class TestStrComp(UnitTestBase, unittest.TestCase):
35+
def data_provider(self):
36+
return [
37+
([], []),
38+
([['1111']], [['1111']]),
39+
([['1111','2222'], ['3333','4444']], [['3333','1111'], ['4444','2222']]),
40+
([['1111','2222','3333'], ['4444','5555','6666'], ['7777','8888','9999']],
41+
[['7777','4444','1111'], ['8888','5555','2222'], ['9999','6666','3333']],
42+
),
43+
([['1111','2222','3333', '4444'],
44+
['5555','6666','7777', '8888'],
45+
['9999','0000','aaaa', 'bbbb'],
46+
['cccc','dddd','eeee', 'ffff']],
47+
[['cccc','9999','5555', '1111'],
48+
['dddd','0000','6666', '2222'],
49+
['eeee','aaaa','7777', '3333'],
50+
['ffff','bbbb','8888', '4444']],
51+
),
52+
]
53+
54+
def func_provider(self):
55+
return [
56+
rotate_matrix,
57+
]
58+
59+
def single_test(self, func, data):
60+
cpy = copy.deepcopy(data)
61+
matrix = data[0]
62+
func(matrix)
63+
self.expect(matrix, func, cpy)
64+
65+
if __name__ == '__main__':
66+
unittest.main()
Binary file not shown.

‎Cracking_the_coding_interview/v6/test_utils.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from abc import ABC, abstractmethod
2+
import copy
23

34
class UnitTestBase(ABC):
45

@@ -10,15 +11,18 @@ def data_provider(self):
1011
def func_provider(self):
1112
pass
1213

13-
@abstractmethod
1414
def func_eval(self, func, args):
1515
pass
1616

1717
def test(self):
1818
for func in self.func_provider():
1919
for data in self.data_provider():
20-
args = data[0]
21-
self.expect(self.func_eval(func, args), func, data)
20+
self.single_test(func, data)
21+
22+
def single_test(self, func, data):
23+
args = data[0]
24+
cpy = copy.deepcopy(data)
25+
self.expect(self.func_eval(func, args), func, cpy)
2226

2327
def expect(self, result, func, data):
2428
expected = data[1]

‎Python_utils.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
5) bitwise operators: x<<y, x>>y, x&y, x|y, ~x, x^y
3636
6) for i in range(len(lst)-1,-1,-1):
3737
7) {char:[] for char in Set}
38-
8) min(lst, key=foo)
38+
8) min(lst, key=foo); min(string_1, string2, len)
3939
9) min(result_l,result_r,key=lambda x:x[0])
4040
10) lst = [[k,v] for k,v in dic.items()]
4141
11) lst = filter(lambda item: item != '', lst)

0 commit comments

Comments
 (0)