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
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:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.