curriculum/challenges/english/blocks/daily-coding-challenges-python/691b559495c5cb5a37b9b485.md
Given a matrix (array of arrays) representing the current state in Conway's Game of Life, return the next state of the matrix using these rules:
1 (alive) or 0 (dead).Rules for updating each cell:
For example, given:
[
[0, 1, 0],
[0, 1, 1],
[1, 1, 0]
]
return:
[
[0, 1, 1],
[0, 0, 1],
[1, 1, 1]
]
Each cell updates according to the number of live neighbors. For instance, [0][0] stays dead (2 live neighbors), [0][1] stays alive (2 live neighbors), [0][2] dies (3 live neighbors), and so on.
game_of_life([[0, 1, 0], [0, 1, 1], [1, 1, 0]]) should return [[0, 1, 1], [0, 0, 1], [1, 1, 1]].
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(game_of_life([[0, 1, 0], [0, 1, 1], [1, 1, 0]]), [[0, 1, 1], [0, 0, 1], [1, 1, 1]])`)
}})
game_of_life([[1, 1, 0, 0], [1, 0, 1, 0], [0, 1, 1, 1], [0, 0, 1, 0]]) should return [[1, 1, 0, 0], [1, 0, 0, 1], [0, 0, 0, 1], [0, 1, 1, 1]].
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(game_of_life([[1, 1, 0, 0], [1, 0, 1, 0], [0, 1, 1, 1], [0, 0, 1, 0]]), [[1, 1, 0, 0], [1, 0, 0, 1], [0, 0, 0, 1], [0, 1, 1, 1]])`)
}})
game_of_life([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) should return [[0, 0, 0], [0, 1, 0], [0, 0, 0]].
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(game_of_life([[1, 0, 0], [0, 1, 0], [0, 0, 1]]), [[0, 0, 0], [0, 1, 0], [0, 0, 0]])`)
}})
game_of_life([[0, 1, 1, 0], [1, 1, 0, 1], [0, 1, 1, 0], [0, 0, 1, 0]]) should return [[1, 1, 1, 0], [1, 0, 0, 1], [1, 0, 0, 1], [0, 1, 1, 0]].
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(game_of_life([[0, 1, 1, 0], [1, 1, 0, 1], [0, 1, 1, 0], [0, 0, 1, 0]]), [[1, 1, 1, 0], [1, 0, 0, 1], [1, 0, 0, 1], [0, 1, 1, 0]])`)
}})
def game_of_life(grid):
return grid
def game_of_life(grid):
rows, cols = len(grid), len(grid[0])
def count_live_neighbors(r, c):
count = 0
for i in range(r-1, r+2):
for j in range(c-1, c+2):
if 0 <= i < rows and 0 <= j < cols and (i != r or j != c):
count += grid[i][j]
return count
next_state = [row[:] for row in grid]
for r in range(rows):
for c in range(cols):
live_neighbors = count_live_neighbors(r, c)
if grid[r][c] == 1:
if live_neighbors < 2 or live_neighbors > 3:
next_state[r][c] = 0
else:
if live_neighbors == 3:
next_state[r][c] = 1
return next_state