-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_of_life_test.py
54 lines (43 loc) · 1.78 KB
/
game_of_life_test.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
# https://leetcode.com/problems/game-of-life
from core.problem_base import *
class GameOfLife(ProblemBase):
def Solution(self, board: list[list[int]]) -> list[list[int]]:
"""
Do not return anything, modify board in-place instead.
"""
directions = [
[ -1, -1 ], [ -1, 0 ], [ -1, 1 ],
[ 0, -1 ], [ 0, 1 ],
[ 1, -1 ], [ 1, 0 ], [ 1, 1 ],
]
count = len(directions)
rows = len(board)
cols = len(board[0])
for row in range(rows):
for col in range(cols):
neighbors = 0
for i in range(count):
y = row + directions[i][0]
x = col + directions[i][1]
if y >= 0 and y < rows \
and x >= 0 and x < cols \
and (board[y][x] == 1 or board[y][x] >= 20):
neighbors += 1
board[row][col] = neighbors + (10 if board[row][col] == 0 else 20)
for row in range(rows):
for col in range(cols):
neighbors = board[row][col] - (20 if board[row][col] >= 20 else 10)
if neighbors < 2 or neighbors > 3:
board[row][col] = 0
elif neighbors == 3 and board[row][col] < 20:
board[row][col] = 1
elif board[row][col] >= 20:
board[row][col] = 1
else:
board[row][col] = 0
return board
if __name__ == '__main__':
TestGen(GameOfLife) \
.Add(lambda tc: tc.Param([[0,1,0],[0,0,1],[1,1,1],[0,0,0]]).Result([[0,0,0],[1,0,1],[0,1,1],[0,1,0]])) \
.Add(lambda tc: tc.Param([[1,1],[1,0]]).Result([[1,1],[1,1]])) \
.Run()