Question

Write in Python This program: 1. Correct the compute_cells_state function which receives as parameter an array...

Write in Python This program:

1. Correct the compute_cells_state function which receives as parameter an array of 2-dimensional booleans. The table is indexed online then in column.
   A cell is called "alive" if the Boolean is true otherwise it is said that it is "dead".
   Each cell to 8 "neighbors" (the 4 adjacent cells and the 4 cells diagonally)
   The function modifies each cell according to the following rule:
     - if the cell has 2 or 3 living neighbors she is alive otherwise she is dead
"" "

def compute_cells_state (cells):
   width = len (cells)
   height = len (cells [0])
   # we are using deepcopy because the Python Repl VM has approximate pointer management
   old_cells = copy.deepcopy (cells)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

SOURCE CODE:

*Please follow the comments to better understand the code.

**Please look at the Screenshot below and use this code to copy-paste.

***The code in the below screenshot is neatly indented for better understanding.

import copy

def calc_count(data,i,j,ROWS,COL):
count=0
if((0<=i<ROWS) and (0<=j+1<COL)) and data[i][j+1]!=False: count+=1

if((0<=i<ROWS) and (0<=j-1<COL)) and data[i][j-1]!=False: count+=1
  
if((0<=i+1<ROWS) and (0<=j+1<COL)) and data[i+1][j+1]!=False:count+=1
  
if((0<=i+1<ROWS) and (0<=j-1<COL)) and data[i+1][j-1]!=False:count+=1
  
if((0<=i+1<ROWS) and (0<=j<COL)) and data[i+1][j]!=False: count+=1
  
if((0<=i-1<ROWS) and (0<=j+1<COL)) and data[i-1][j+1]!=False:count+=1
  
if((0<=i-1<ROWS) and (0<=j-1<COL)) and data[i-1][j-1]!=False:count+=1

if((0<=i-1<ROWS) and (0<=j<COL)) and data[i-1][j]!=False: count+=1
  
return count


def compute_cells_state(cells):
width = len(cells)
height = len(cells[0])
# we are using deepcopy because the Python Repl VM has approximate pointer management
old_cells = copy.deepcopy(cells)

#Start here
for i in range(width):
for j in range(height):
if calc_count(old_cells,i,j,width,height) in (2,3):
cells[i][j]="alive"
else:
cells[i][j]="dead"
print("The result is:")
for row in cells:
print(row)

#Testing the code
cells=[ [True,True,False,True],
[False,False,False,True],
[False,True,True,True],
[True,True,False,False]
]
  
compute_cells_state(cells)

================================================================

SCREENSHOT FOR CODE:

OUTPUT:

EXAMPLE EXPLANATION:

Add a comment
Know the answer?
Add Answer to:
Write in Python This program: 1. Correct the compute_cells_state function which receives as parameter an array...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1)def toggle_cell(row, column, cells): • Return value: Flip the cell in the column of row from...

    1)def toggle_cell(row, column, cells): • Return value: Flip the cell in the column of row from alive to dead or vice versa. Return True if the toggle was successful, False otherwise. • Assumptions: o cells will be a two-dimensional list with at least one row and one element in that row. o row and column are index values of cells • Notes: o Cells is being edited in place. Note that the return value is a Boolean and not a...

  • I'm working on the following: Write a python program: 1. Write the definition of the function...

    I'm working on the following: Write a python program: 1. Write the definition of the function displaySubMenu that displays the following menu; this function doesn't collect the user's input; only display the following menu.: [S]top – Press 'S' to stop. 2. Write the definition of the function setNextGenList that creates a pattern of next generation (tempGen) based on the current generation (currentGen); modify the tempGen based on the currentGen by applying the rules of Game of Life. Conway's Game of...

  • Objective: Write a program that implements the Game of Life cellular automata system invented by John...

    Objective: Write a program that implements the Game of Life cellular automata system invented by John Conway. 1. Create two game grids of size at least 50x50. These grid cells can be either Boolean or integer. In the following, I’ll refer to these as gridOne and gridTwo. 2. Set all cells in both grids to false. 3. Start by initializing gridOne. Allow the user to specify two different ways of initializing the grid: 1) by specifying a pattern file to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT