-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsol.py
78 lines (63 loc) · 1.97 KB
/
sol.py
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
from typing import List
import pytest
class Solution:
def rotateTheBox(self, box: List[List[str]]) -> List[List[str]]:
for i in range(len(box)):
row = box[i]
box[i] = self.dropTo(row)
return self.rotate_mat_90_deg(box)
def dropTo(self, arr: List[int]) -> None:
drop_to_idx = None
for i in range(len(arr) - 1, -1, -1):
cur = arr[i]
if cur == ".":
if drop_to_idx is None:
drop_to_idx = i
elif cur == "*":
drop_to_idx = None
else: # #
if drop_to_idx is None:
continue
arr[drop_to_idx] = "#"
arr[i] = "."
drop_to_idx -= 1
return arr
def rotate_mat_90_deg(self, mat) :
rows = len(mat)
cols = len(mat[0])
new_mat = []
for c in range(cols):
new_row = []
for r in range(rows):
cur = mat[r][c]
new_row.insert(0, cur)
new_mat.append(new_row)
return new_mat
s = Solution()
arr = s.rotateTheBox([
["#",".","*","."],
["#","#","*","."]
])
# arr = s.dropTo(["#","#","."])
# print(arr)
def test_full():
arr = ["#","#"]
expected = ["#","#"]
assert s.dropTo(arr) == expected
def test_free_fall():
arr = ["#", "#", ".", ".", ".", ".", "."]
expected = [".",".",".",".",".","#","#"]
assert s.dropTo(arr) == expected
def test_free_fall_with_space():
arr = ["#", ".", ".", "#", ".", ".", "."]
expected = [".",".",".",".",".","#","#"]
assert s.dropTo(arr) == expected
def test_fall_to_obstacle():
arr = ["#", ".", ".", "*", ".", ".", "."]
expected = [".",".","#","*",".",".","."]
assert s.dropTo(arr) == expected
def test_fall_to_obstacle_and_more():
arr = ["#", ".", "*", ".", "#", ".", "."]
expected = [".","#","*",".",".",".","#"]
assert s.dropTo(arr) == expected
# pytest.main()