Question

For PYTHON you must design and program a function that receives a square matrix of 0s...

For PYTHON you must design and program a function that receives a square matrix of 0s and 1s of arbitrary size and a generation number.
Each cell in the automaton is represented by an element of the matrix with 0 if it is dead or 1 if it is alive. A generation is defined as the application of the rules of the cellular automaton to each cell, generating a new matrix with the results. The function to be programmed must return to the last generation required by the second argument of the function.
Note: your function must accept any matrix from size 1
Valid generations are positive integers greater than or equal to 0

PYTHON Program

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

Here is the solution. let me know if you need any clarification or face any problems

Source : CellularAutomaton.py
def compute_generation(cell_matrix:[[]], generation:int):
    dead = 0
    alive = 1
    if generation == 0:
        return cell_matrix
    # create a temporary board
    generation_matrix = [[] for i in range(len(cell_matrix))]
    for i in range(len(cell_matrix)):
        generation_matrix[i] = []
        for j in range(len(cell_matrix[0])):
            neighbours = count_neighbours(cell_matrix,i,j)
            # if the cell is dead
            if cell_matrix[i][j] == dead:
                # if the dead cell has 3 exactly neighbours
                if neighbours == 3:
                    # cell becomes alive by reproduction
                    cell = alive
                else:
                    # cell remain dead
                    cell = dead
            else:
                # if alive neighbour cell count less than 2 or greater than 3
                if neighbours < 2 or neighbours > 3:
                    # cell dies either by loneliness or starvation due to over population
                    cell = dead
                else:
                    # cell survives to next generation
                    cell = alive
            # set cell status in generation matrix
            generation_matrix[i].append(cell)
    # recursive call to
    cell_matrix = compute_generation(generation_matrix, generation - 1)
    return cell_matrix

def get_cell (cell_matrix:[[]],row:int, col:int):
    # if row or col is out of bound of matrix, return 0, indicating dead cell or no cell
    if row < 0 or row >= len(cell_matrix) or col < 0 or col >= len(cell_matrix[0]):
        return 0
    # else return the value at the position
    return cell_matrix[row][col]

def count_neighbours(cell_matrix:[[]],row:int, col:int):
    # neighbour counter
    count = 0
    # checking 8 neighbour cells
    if get_cell(cell_matrix,row-1,col-1) != 0 :
        count += 1
    if get_cell(cell_matrix,row-1,col)!=0:
        count +=1
    if get_cell(cell_matrix,row-1,col+1)!=0:
        count +=1
    if get_cell(cell_matrix,row,col-1)!=0:
        count +=1
    if get_cell(cell_matrix,row,col+1)!=0:
        count +=1
    if get_cell(cell_matrix,row+1,col-1)!=0:
        count +=1
    if get_cell(cell_matrix,row+1,col)!=0:
        count +=1
    if get_cell(cell_matrix,row+1,col+1)!=0:
        count +=1
    # return neighbor count
    return count

if __name__ =="__main__":
    cell_matrix = []
    cell_matrix.append([0,0,0,0,0,0,0,0,0])
    cell_matrix.append([0,0,0,0,0,0,0,0,0])
    cell_matrix.append([0,0,0,0,1,1,1,0,0])
    cell_matrix.append([0,0,1,0,0,0,0,1,0])
    cell_matrix.append([0,0,1,0,0,0,0,1,0])
    cell_matrix.append([0,0,1,0,0,0,0,1,0])
    cell_matrix.append([0,0,0,0,0,0,0,0,0])
    cell_matrix.append([0,0,0,0,1,1,1,0,0])
    cell_matrix.append([0,0,0,0,0,0,0,0,0])
    cell_matrix.append([0,0,0,0,0,0,0,0,0])
    print("====================================")
    print("Generation : ",0)
    print("====================================")
    for i in range(len(cell_matrix)):
        print(cell_matrix[i])
    # compute generation
    generation = 3
    cell_matrix = compute_generation(cell_matrix, generation)
    print("====================================")
    print("Generation : ",generation)
    print("====================================")
    for i in range(len(cell_matrix)):
        print(cell_matrix[i])
Screens: Source

Screens: Output
Add a comment
Know the answer?
Add Answer to:
For PYTHON you must design and program a function that receives a square matrix of 0s...
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
  • 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...

  • Python 3.7 to be used. Just a simple design a program that depends on its own. You should also not need to import anythi...

    Python 3.7 to be used. Just a simple design a program that depends on its own. You should also not need to import anything. No code outside of a function! Median List Traversal and Exception Handling Create a menu-driven program that will accept a collection of non-negative integers from the keyboard, calculate the mean and median values and display those values on the screen. Your menu should have 6 options 50% below 50% above Add a number to the list/array...

  • 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...

  • You will write a Java program that implements Conway’s Game of Life, a simple cellular automaton...

    You will write a Java program that implements Conway’s Game of Life, a simple cellular automaton discussed in class. See for example: http://www.bitstorm.org/gameoflife/ Our version has a 10 x 10 grid, numbered like this: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 The grid is represented by a 10 x 10 2­dimensional integer array. If the grid point (i, j) is "populated", the array element [i][j] contains 1;...

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