A petri dish is represented as a grid of size , 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
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 . |
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__":
>>> 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', '.']]