Question

def count_neighbors(cells, row, col): • Return value: An integer; the number of "alive" neighbors that the...

def count_neighbors(cells, row, col):

• Return value: An integer; the number of "alive" neighbors that the cell in the given row and column has.

• Assumptions: cells will be a two-dimensional list with at least one row and one element in that row.

• Notes: The tests for this function are hidden. Be careful: row or column may not be valid (return -1 if so)

• Examples:

count_neighbors([[0,0,0],[0,1,0],[0,0,0]],1,1) → 0

count_neighbors([[0,1,0,0],[0,1,0,1],[1,1,0,1]],2,1) → 3

count_neighbors([[0,0,0],[0,1,0],[0,0,0]],-1,1) → -1

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

Note

######

Index is considered to be starting from 0 ie if the array is 3 *4 size then

                     row index will be 0 to 2

                   column index will be 0 to 3

##################### PGM START ###########################################

#method to find the neighbours which are alive
def count_neighbors(cells, row, col):
    count=0
  
    #check if the row and col values are valid, if not return -1
    if(not((row>=0 and row<len(cells)) and (col>=0 and col<len(cells[row])))):
        return -1

    #check for all the 8 neighbours, if values are 1 incremnt counter by 1
    if row-1>=0 and (cells[row-1][col]==1):
        count+=1
      
    if (row+1<len(cells)) and (cells[row+1][col]==1):
        count+=1
          
    if (col-1>=0) and (cells[row][col-1]==1):
        count+=1
      
    if (col+1<len(cells[row])) and (cells[row][col+1]==1):
        count+=1
      
    if (col+1<len(cells[row]) and row+1<len(cells)) and (cells[row+1][col+1]==1):
        count+=1
      
    if (col+1<len(cells[row]) and row-1>=0) and (cells[row-1][col+1]==1):
        count+=1
      
    if (row+1<len(cells) and col-1>=0) and (cells[row+1][col-1]==1):
        count+=1
      
    if (col-1>=0 and row-1>=0) and (cells[row-1][col-1]==1):
        count+=1
  
    #return final count
    return count

#main method
if __name__=="__main__":
    print(count_neighbors([[0,0,0],[0,1,0],[0,0,0]],1,1))
    print(count_neighbors([[0,1,0,0],[0,1,0,1],[1,1,0,1]],2,1))
    print(count_neighbors([[0,0,0],[0,1,0],[0,0,0]],-1,1))
    print(count_neighbors([[1,1,1,1],[1,1,1,1],[1,1,1,1]],1,2))
   

####################### PGM END ###########################################

OUTPUT
##########

Add a comment
Know the answer?
Add Answer to:
def count_neighbors(cells, row, col): • Return value: An integer; the number of "alive" neighbors that the...
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...

  • can someone indent this code correctly in python programming def count_neighbors(cells,row,col): rows=len(cells) #storing no. of rows...

    can someone indent this code correctly in python programming def count_neighbors(cells,row,col): rows=len(cells) #storing no. of rows cols=len(cells[0]) #storing no. of columns if(row<0 or col<0 or row>rows-1 or col>cols-1): # when row or column is out of range return -1 count=0 if(row==0 and cells[rows-1][col]==1): #cyclic order count+=1 if(col==0 and cells[row][cols-1]==1): count+=1 if(row==rows-1 and cells[0][col]==1): count+=1 if(col==cols-1 and cells[row][0]==1): count+=1 if(col>=1 and cells[row][col-1]==1): #left neighbor count+=1 if (row>=1 and cells[row-1][col]==1): #upper neighbor count+=1 if(row+1<rows and cells[row+1][col]==1): #down neighbor count+=1 if(col+1<cols and cells[row][col+1]==1):...

  • Def snake (grid, row, co1): Implement a function to calculate the longest "snake" of ones that ca...

    def snake (grid, row, co1): Implement a function to calculate the longest "snake" of ones that can be made from a starting position in a grid. A snake is made by moving right and down from a starting position as far as possible. o Assumption: grid will be at least 1x1, row and col will each be valid indexes in grid o Restrictions: You may not use the max() or min) functions. o Examples: (cells is a 2D list below)...

  • Make a program using Java that asks the user to input an integer "size". That integer...

    Make a program using Java that asks the user to input an integer "size". That integer makes and prints out an evenly spaced, size by size 2D array (ex: 7 should make an index of 0-6 for col and rows). The array must be filled with random positive integers less than 100. Then, using recursion, find a "peak" and print out its number and location. (A peak is basically a number that is bigger than all of its "neighbors" (above,...

  • You must use recursion to solve each problem. You cannot use loops in this homework. You...

    You must use recursion to solve each problem. You cannot use loops in this homework. You cannot import any module. A couple of the tasks have individual restrictions; note them, as we will remove points for any task that does not follow the requirements. def factorial_evens(num): Implement a function to calculate and return the product of all even numbers from 1 up to num (inclusive if num is even). o Assumption: num will be an integer greater than or equal...

  • You are going to be implementing the classic computer science simulation, Conway's Game of Life. Conway's...

    You are going to be implementing the classic computer science simulation, Conway's Game of Life. Conway's Life is played on a matrix of cells, kind of like a chess board but theoretically extending infinitely in every direction. Each individual cell in the matrix can either be alive or dead. A live cell in the matrix is shown in our simulation by printing an asterisk (*) to the screen. A dead cell is shown by leaving that area of the matrix...

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