Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

please use the python language, preferabally 2.7 for this, and you will be const

ID: 3874412 • Letter: P

Question

please use the python language, preferabally 2.7 for this, and you will be constructing if-elif-else statement to meet the condition of the tile that the grid would be asking. Please be sure it would work for all inputs.   The parameters and answer template for the solution is also given beneath the screenshot below

Here below is the answer template or parameters for this problem

def findNeighbors(width, height, tile):
    width = width
    height = height
    tile = tile


    #YOUR CODE GOES HERE (indented)

  
    #END YOUR CODE

Imagine that the user specifies with width and height of a grid, and provides a tile in that grid. You will write code to return all of the tiles adjacent to the tile provided by the user in string format separated by spaces. There should be an additional space at the end of the string you will return (this will make your algorithm easier to write). For example, on the 7x10 grid below, 16 17 18 30 3132 57 58 65 tiles 24 has the neighbors "16 17 18 23 25 30 3132If a tile is at the edge of the grid, such as tile 64, we only include its adjacent neighbors 57 58 65 ".Make sure you return the tiles in sorted order like in these examples you don't need to sort them formally, just add them into the string in the correct order) If a tile has no neighbors, you should return an empty string

Explanation / Answer

def findNeighbors(width, height, tile): #function definition
    k = 1 #k to store value values in list
    l = [] #list to store all the tiles
    for i in range(0,height):   #generating list
      r = [] #for each row of tiles
      for j in range(0,width):
        r.append(k)
        k += 1
      l.append(r)
    row = (tile - 1) // width #for calculating row number with tile value
    col = (tile - 1) % width #for calculating column number with tile value
    res = "" #variable fr storing string
    #checking if adjacent positions are valid if they are valid appending value at that position to list
    if row > 0:
      if col > 0:
        res += str(l[row-1][col-1]) + " "
      res += str(l[row-1][col]) + " "
      if col < width:
        res += str(l[row-1][col+1]) + " "
    if col > 0:
      res += str(l[row][col-1]) + " "
    if col < width:
      res += str(l[row][col+1]) + " "
    if row <width:
      if col > 0:
        res += str(l[row+1][col-1]) + " "
      res += str(l[row+1][col]) + " "
      if col < width:
        res += str(l[row+1][col+1]) + " "
    return res
  
print findNeighbors(7,10,24)
print findNeighbors(7,10,64)

output: