Prokaryote Growths (2) U72212


Statement
 

pdf   zip   main.py

A petri dish is represented as a grid of size N×MN\times{M}, where:

  • B represents a bacterium

  • . represents an empty space

At each growth cycle, any bacteria spreads to its neighbouring positions in diagonal (up-left, up-right, down-left, down-right). Newborn bacteria are represented in lowercase (’b’)

Write a function growth_cycle(grid) that, given an N×MN\times{M} matrix representing the initial state of the colony, returns the state of the petri dish after one growth cycle.

EXAMPLES

Example 1
Inital state       After 1 cycle (new cells in lowercase)
. . . . . B . . . b . b B .
. . . B . . . . b . B b . b
B . . . . . . B . b . b . b
. . . . . B . . b . . . B .
. . . . . . . . . . . b . b

 

Example 2
Inital state       After 1 cycle (new cells in lowercase)
. . . . . . . . . . b . b . . .
. . . B . . . . . . . B . b . .
. . . . B . . . . b b . B . . .
B . . . . . . . B . . b . b . .

 

Example 3
Inital state       After 1 cycle (new cells in lowercase)
. . . . . . b . b . b .
. B . B . . . B . B . .
B . . . . . B . b . b .
. . . . . B . b . . . B
. . . . . B . . . . b B
. . . . . . . . . . b .

Observation

  • Process the dish positions from left to right and from top down. In case a cell is already occupied, it must not be overwritten with a newborn bacterium.

  • It may be useful to write a function inside(grid,p,q) that returns True if position (p,q) is inside the limits of grid, and False otherwise.

Important: Submit only the function. If you have a main program, comment it out or embed it inside a conditional clause if __name__ == "__main__":

Sample session
>>> growth_cycle([['.', '.', '.', '.', '.', 'B', '.'], ['.', '.', '.', 'B', '.', '.', '.'], ['B', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', 'B', '.'], ['.', '.', '.', '.', '.', '.', '.']])
[['.', '.', 'b', '.', 'b', 'B', '.'], ['.', 'b', '.', 'B', 'b', '.', 'b'], ['B', '.', 'b', '.', 'b', '.', 'b'], ['.', 'b', '.', '.', '.', 'B', '.'], ['.', '.', '.', '.', 'b', '.', 'b']]
>>> growth_cycle([['.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', 'B', '.', '.', '.', '.'], ['.', '.', '.', '.', 'B', '.', '.', '.'], ['B', '.', '.', '.', '.', '.', '.', '.']])
[['.', '.', 'b', '.', 'b', '.', '.', '.'], ['.', '.', '.', 'B', '.', 'b', '.', '.'], ['.', 'b', 'b', '.', 'B', '.', '.', '.'], ['B', '.', '.', 'b', '.', 'b', '.', '.']]
>>> growth_cycle([['.', '.', '.', '.', '.', '.'], ['.', 'B', '.', 'B', '.', '.'], ['B', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', 'B'], ['.', '.', '.', '.', '.', 'B'], ['.', '.', '.', '.', '.', '.']])
[['b', '.', 'b', '.', 'b', '.'], ['.', 'B', '.', 'B', '.', '.'], ['B', '.', 'b', '.', 'b', '.'], ['.', 'b', '.', '.', 'b', 'B'], ['.', '.', '.', '.', 'b', 'B'], ['.', '.', '.', '.', 'b', '.']]
Information
Author
Lluís Padró
Language
English
Official solutions
Python
User solutions
Python