Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Question 1 The 1-dimensional Game of Life occurs on a list of cells where each cell is either alive or dead. The population of cells

Question 1

The 1-dimensional Game of Life occurs on a list of cells where each cell is either alive or dead. The population of cells evolves from one generation to the next according to three rules:

  1. Any live cell survives into the next generation if it has 2 or 4 living neighbours in its 2-neighborhood (the cell itself does not count towards the number of living neighbors).
  2. Any dead cell becomes alive in the next generation if it has 2 or 3 living neighbours in its 2-neighborhood.
  3. All other live cells die and all other dead cells remain dead.

The rules are applied to each cell simultaneously to compute the next generation of cells.

The 2-neighborhood of a cell consists of the cell itself and its two neighbors to the left and two neighbors to the right of the cell (if those neighbors exist). The 2-neighborhoods for three different cells labelled i are shown in the figure below. Cells near the front or back of the list may have fewer than 5 cells in their 2-neighborhood.

Complete the Python module named life1.py that provides functions useful for implementing the 1-dimensional Game of Life. At a minimum, the module should provide the following functions:

  • make_cells(n, val)
  • print_cells(cells)
  • neighborhood(cells, index)
  • evolve(cells)
  • blinker(cells, index)

You may add additional functions (such as a function that determines if a dead cell becomes alive in the next generation, or function that determines if an alive cell survives in the next generation) if you wish.

It is strongly recommended that you complete the functions in the order that they are listed above. To help you test your work, the examples shown below are included in the starter code provided to you. You are strongly encouraged to write your own tests.

Each function in your module should be documented using the convention described in the Week 05 Jupyter notebook Functions: Documentation.

Each function is described below.

make_cells (note that this function is already implemented for you)

make_cells(n, val): Returns a list of length n where all of the elements are equal to the boolean value val. Raises a ValueError if n is less than zero.

Example

c = life1.make_cells(5, False)

should return the list [False, False, False, False, False].

print_cells

print_cells(cells): Prints a list cells where all of the elements of cells are equal to either True or False. Use # for True and - for False. Include a newline after printing the cells.

Example

# this is ex_print_cells.py c = life1.make_cells(7, False) c[3] = True c[5] = True life1.print_cells(c) life1.print_cells(c)

should print:

---#-#- ---#-#-

neighborhood

neighborhood(cells, index): Returns a list containing the cells in the 2-neighborhood for the cell cells[index]. Raises a ValueError if index is less than zero.

Example

# this is ex_neighborhood.py c = life1.make_cells(7, False) c[3] = True c[5] = True print('cells') life1.print_cells(c) print('neighborhood(c, 0)') n = life1.neighborhood(c, 0) life1.print_cells(n) print('neighborhood(c, 1)') n = life1.neighborhood(c, 1) life1.print_cells(n) print('neighborhood(c, 3)') n = life1.neighborhood(c, 3) life1.print_cells(n) print('neighborhood(c, 6)') n = life1.neighborhood(c, 6) life1.print_cells(n)

should print:

cells ---#-#- neighborhood(c, 0) --- neighborhood(c, 1) ---# neighborhood(c, 3) --#-# neighborhood(c, 6) -#-

evolve

evolve(cells): Applies the rules of the 1-dimensional Game of Life to cells to get the next generation of cells. Replaces each cell in cells with the new generation. Raises a ValueError if cells is empty.

Example

# this is ex_evolve.py c = life1.make_cells(7, False) c[3] = True c[5] = True c[6] = True print('cells') life1.print_cells(c) life1.evolve(c) print('evolve') life1.print_cells(c)

should print:

cells ---#-## evolve ----##-

Explanation

  • the first cell remains dead because it has 0 alive neighbors (in its 2-neighborhood)
  • the second cell remains dead because it has 1 alive neighbor (in its 2-neighborhood)
  • the third cell remains dead because it has 1 alive neighbor (in its 2-neighborhood)
  • the fourth cell dies because it has 1 alive neighbor (in its 2-neighborhood)
  • the fifth cell becomes alive because it has 3 alive neighbors (in its 2-neighborhood)
  • the sixth cell remains alive because it has 2 alive neighbors (in its 2-neighborhood)
  • the seventh cell dies because it has 1 alive neighbor (in its 2-neighborhood)

Implementation note: The rules are supposed to be applied to all cells simultaneously but you cannot do this in Python. Practically, what this means is that you cannot change a value in cells until that value is no longer in the 2-neighborhood of any other cell that you need to update (e.g., you cannot change cells[0] until you have computed the results for cells[1] and cells[2]). There are several different ways to deal with this problem, but the easiest solution is to first copy the elements of the list cells into a new temporary list and then use the temporary list for counting neighbors. You can then update the values in the list cells without issue.

You can run ex_life1.py to produce the figure shown in the Background Information section of this assignment.

blinker

blinker(cells, index): Inserts a blinker pattern into the list cells with the pattern starting at cells[index]. Raises a ValueError if index is less than zero or if the complete blinker pattern does not fit into cells when inserted at index.

The blinker pattern is made up of the six boolean values False, False, True, True, False, False. All six values must fit into cells starting at cells[index] otherwise blinker(cells, index) will raise an exception.

Example

# this is ex_blinker.py c = life1.make_cells(12, False) print('cells') life1.print_cells(c) life1.blinker(c, 3) print('cells with blinker') life1.print_cells(c) for i in range(0, 10): life1.print_cells(c) life1.evolve(c)

should print

cells ------------ cells with blinker -----##----- -----##----- ----#--#---- -----##----- ----#--#---- -----##----- ----#--#---- -----##----- ----#--#---- -----##----- ----#--#----

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Illuminated

Authors: Catherine Ricardo

2nd Edition

1449606008, 978-1449606008

More Books

Students also viewed these Databases questions