id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

peter@norvig.com life > life
prev  |  next  |  chance

As Wikipedia says: The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970. The "game" is a zero-player game, meaning that its evolution is determined by its initial state, requiring no further input. One interacts with the Game of Life by creating an initial configuration and observing how it evolves. The world of Life is an infinite two-dimensional grid of square cells, each of which is in one of two possible states, alive or dead. Every cell interacts with its eight neighbors (horizontally, vertically, or diagonally). At each step in time, the rules are: Any live cell with fewer than two live neighbors or more than three live neighbors dies (of loneliness or overpopulation). Any live cell with two or three live neighbors lives on to the next generation. Any dead cell with exactly three live neighbors becomes a live cell in the next generation. There are two good representations of a set of cells. One is to have a matrix, M, where M[x, y] is 1 or True when the cell is alive. The problem with this is we don't know ahead of time what size to make the matrix M. The other representation is to have a list of points listing all the live cells. So, [(0, 0), (1, 0)] means there are two live cells, one at the origin and one just to the right of it. Your task is to write the function life(cells), which takes as input a list of cells, in (x, y) coordinate format, and returns the result of running the rules for just one generation. The result should be a list of (x, y) coordinates sorted in standard Python sort order.


...Save, Compile, Run (ctrl-enter)

def life(cells):

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Python Help

Post-solution available

Copyright Nick Parlante 2017 - privacy