This repository was archived by the owner on Sep 22, 2021. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change
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
+
You can’t perform that action at this time.
0 commit comments