Skip to content
DeepForgeby svx
All problems
MediumAlgorithmsal-242

Game of Life

Problem

Given a board of 0 and 1, return the next state of Conway's Game of Life.

A live cell survives with two or three live neighbours, a dead cell becomes live with exactly three, and every other cell dies or stays dead. Count all eight neighbours of each cell and build a new board.

Hint

Build a fresh board so updates do not affect neighbour counts within the same generation.

Test cases

case 1
in: [[[0,1,0],[0,0,1],[1,1,1],[0,0,0]]]
expected: [[0,0,0],[1,0,1],[0,1,1],[0,1,0]]
case 2
in: [[[1,1],[1,0]]]
expected: [[1,1],[1,1]]
case 3
in: [[[0]]]
expected: [[0]]
case 4
in: [[[1]]]
expected: [[0]]
case 5
in: [[[]]]
expected: [[]]
Starter codePython
def game_of_life(board):
    # Your code here
    pass

5 test cases · run your code against them in the editor