Question

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): #right neighbor
count+=1
if(row+1<rows and col+1<cols and cells[row+1][col+1]==1): #diagonal right down
count+=1
if(row+1<rows and col>=1 and cells[row+1][col-1]==1): #diagonal left down
count+=1
if(row>=1 and col+1<cols and cells[row-1][col+1]==1): #diagonal up right
count+=1
if(row>=1 and col>=1 and cells[row-1][col-1]==1): #diagonal left up
count+=1
return count

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

1 def count_neighbprs (cells, row, col): rows-len(cells) #storing no. of cols-len (cells [0]) #storing no. if (row<0 or col<0

Add a comment
Know the answer?
Add Answer to:
can someone indent this code correctly in python programming def count_neighbors(cells,row,col): rows=len(cells) #storing no. of rows...
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
  • 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

  • Need help with a 2D list - I'm almost there! #Write a function called check_winner which...

    Need help with a 2D list - I'm almost there! #Write a function called check_winner which takes #as input a 2D list. It should return "X" if there are four #adjacent "X" values anywhere in the list (row, column, #diagonal); "O" if there are four adjacent "O" values #anywhere in the list; and None if there are neither. # #Here are the ways Connect-4 is different from tic-tac-toe: # # - Connect-4 is played with 6 rows and 7 columns...

  • Throughout this script, can you provide helpful comments about how each function works in the script...

    Throughout this script, can you provide helpful comments about how each function works in the script plus it's significance to the alignment process. Also are there any errors in this script or a way to improve this script? Any help would be appreciated. Thank you. THIS IS A PYTHON CODE AND IS IN IT'S FORMAT _ ITS CLEAR! #!/usr/bin/env python # file=input("Please provide fasta file with 2 sequences:") match=float(input('What is the match score?:')) missmatch=float(input('What is the missmatch score?:')) gap=float(input('What is...

  • 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 I get help with adding the following to this code please? 1. When buying multiple...

    Can I get help with adding the following to this code please? 1. When buying multiple tickets, if one of the seats selected is already taken, give a message like "Sorry, one or more of the seats selected is already taken." and prompt the user to select the seats all over. (Or if possible to make it suggest a location where the seats are together that the user can select instead) 2. Print the total cost after all of the...

  • python programming: Can you please add comments to describe in detail what the following code does:...

    python programming: Can you please add comments to describe in detail what the following code does: import os,sys,time sl = [] try:    f = open("shopping2.txt","r")    for line in f:        sl.append(line.strip())    f.close() except:    pass def mainScreen():    os.system('cls') # for linux 'clear'    print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")    print(" SHOPPING LIST ")    print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")    print("\n\nYour list contains",len(sl),"items.\n")    print("Please choose from the following options:\n")    print("(a)dd to the list")    print("(d)elete from the list")    print("(v)iew the...

  • For this lab, you will work with two-dimensional lists in Python. Do the following: Write a...

    For this lab, you will work with two-dimensional lists in Python. Do the following: Write a function that returns the sum of all the elements in a specified column in a matrix using the following header: def sumColumn(matrix, columnIndex) Write a function display() that displays the elements in a matrix row by row, where the values in each row are displayed on a separate line. Use a single space to separate different values. Sample output from this function when it...

  • To mimic a real-world programming situation, you are given partially-completed code, written by a former employee you...

    To mimic a real-world programming situation, you are given partially-completed code, written by a former employee you have just replaced. Your task is to complete the program he was developing. Fortunately, the former employee's documentation is up-to-date and quite useful Below is the incomplete section of the code, taken from the Python program which is to use Turtle to draw a grid. (In this exercise, a gridis a series of rows, and a rowis a series of boxes or squares.)...

  • Here is my code for minesweeper in python and it has something wrong. Could you please help me to fix it? import tkinter as tk import random class Minesweeper: def __init__(self): self.main = tk.Tk()...

    Here is my code for minesweeper in python and it has something wrong. Could you please help me to fix it? import tkinter as tk import random class Minesweeper: def __init__(self): self.main = tk.Tk() self.main.title("mine sweeper") self.define_widgets() self.mines = random.sample( [(i,j) for i in range(25) for j in range(50) ],self.CustomizeNumberOfMines()) print(self.mines) self.main.mainloop() self.CustomizeNumberOfMines() def define_widgets(self): """ Define a canvas object, populate it with squares and possible texts """ self.canvas = tk.Canvas(self.main, width = 1002, height=502, bg="#f0f0f0") self.canvas.grid(row=0, column=0) self.boxes =...

  • Python code. NO importing modules. you may not use: contains(), index(), sum(),join(), split(). def column_cipher(plaintext): •...

    Python code. NO importing modules. you may not use: contains(), index(), sum(),join(), split(). def column_cipher(plaintext): • Parameter(s): plaintext ----- a string; the message to be encrypted • Return value: A string; the ciphertext after applying the column cipher algorithm • Assumptions: o The standard English alphabet is used: "abcdefghijklmnopqrstuvwxyz" o All strings will be non-empty, entirely lowercase, and only contain valid letters in the alphabet. • How it works: o First, transpose the text into 5 different columns by writing...

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