Skip to content
This repository was archived by the owner on Sep 22, 2021. It is now read-only.

Commit e3cf5df

Browse files
authored
Uploaded code
1 parent 21fb652 commit e3cf5df

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

LeetCode/0289_Game_of_Life.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class Solution:
2+
def gameOfLife(self, board):
3+
self.m = len(board)
4+
self.n = len(board[0])
5+
self.board = [rows.copy() for rows in board]
6+
for i in range(self.m):
7+
for j in range(self.n):
8+
state = self.board[i][j]
9+
dataDict = self.CheckNeighbors(i,j)
10+
nextGen = self.NextGenState(state,dataDict)
11+
board[i][j] = nextGen
12+
13+
14+
def CheckNeighbors(self,row,column):
15+
retDict = {"live":0,"dead":0}
16+
lb = max(column-1,0)
17+
rb = min(column+2,self.n)
18+
tb = max(row -1,0)
19+
bb = min(row+2,self.m)
20+
for rows in range(tb,bb):
21+
for columns in range(lb,rb):
22+
if rows != row or columns != column :
23+
neighbor = self.board[rows][columns]
24+
if neighbor == 1:
25+
retDict["live"] += 1
26+
else:
27+
retDict["dead"] += 1
28+
return retDict
29+
30+
def NextGenState(self,state,dataDict):
31+
finalState = 0
32+
liveNeighbors = dataDict["live"]
33+
if state == 1:
34+
if liveNeighbors == 2 or liveNeighbors == 3:
35+
finalState = 1
36+
else:
37+
if liveNeighbors == 3:
38+
finalState = 1
39+
return finalState
40+
41+
42+

0 commit comments

Comments
 (0)