question archive In this problem, you should write one function named (get_highest_neighbor)

In this problem, you should write one function named (get_highest_neighbor)

Subject:Computer SciencePrice:4.86 Bought12

In this problem, you should write one function named (get_highest_neighbor). This function should have three parameters. The fist will be a 2D list of numeric values, and the second and third will be integers, representing a zero-based row/column position within the 2D list. This function should look at the 4 "neighbors" of the number at the position specified by the second and third parameters (the neighbor "above" it, "below" it, to the "left" of it, and to the "right" of it). The function should return the largest value of these four neighbors. You can assume that the grid will contain numbers greater than or equal to zero. For instance, the code: grid = [[5, 4, 3, 2, 1], [1, 2, 3, 0, 0], [7, 2, 8, 1, 2], [1, 2, 1, 9, 0]] result = get_highest_neighbor(grid, 1, 2) print(result) result = get_highest_neighbor(grid, 1, 1) print(result) result = get_highest_neighbor(grid, 2, 3) print(result) Should print

pur-new-sol

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE

Answer Preview

grid = [[5,4,3,2,1],

    [1,2,3,0,0],

    [7,2,8,1,2],

    [1,2,1,9,0]]

 

 

def get_highest_neighbor(grid, row, column):

  neighbor_index_list = []

  row_size = len(grid)

  column_size = len(grid[0])

 

  neighboring_row = [row-1, row+1]

  neighboring_column = [column-1, column+1]

 

  for nrow in neighboring_row:

    if(nrow >= 0 and nrow < row_size):

      neighbor_index_list.append((nrow, column))

  for ncolumn in neighboring_column:

    if(ncolumn >= 0 and ncolumn < column_size):

      neighbor_index_list.append((row, ncolumn))

 

  highest_neighbor = 0

  for index in neighbor_index_list:

    grid_value = grid[index[0]][index[1]]

    if grid_value > highest_neighbor:

      highest_neighbor = grid_value

 

  return highest_neighbor

 

result = get_highest_neighbor(grid, 1, 2)

print(result)

result = get_highest_neighbor(grid, 1, 1)

print(result)

result = get_highest_neighbor(grid, 2, 3)

print(result)

Step-by-step explanation

#this is the given grid

grid = [[5,4,3,2,1],

    [1,2,3,0,0],

    [7,2,8,1,2],

    [1,2,1,9,0]]

 

#this function returns the highest neighbor of the indexes given

def get_highest_neighbor(grid, row, column): #function accepts parameters, grid, row(index of row) and column(index of column)

  neighbor_index_list = [] #this will store the neighboring indexes

  row_size = len(grid) #get the size of the row

  column_size = len(grid[0]) #get the size of the column

 

  neighboring_row = [row-1, row+1] #this will get the index of the row below and above of the given row

  neighboring_column = [column-1, column+1] #this will get the index of the column left and right of the given column

 

#get the indexes above and below the given indexes

  for nrow in neighboring_row: #iterate through neighboring_row

    if(nrow >= 0 and nrow < row_size): #check if it is within the limit index, for the given grid since size of row is 4 it should be 0 - 3

      neighbor_index_list.append((nrow, column))#add indexes as a tuple to the neighbor_index_list, notice that the given column is used

"""

for example : row = 1 and column = 2, the value of this indexes in the grid is grid[1][2] = 3

neighboring_row = [0, 2]

since 0 and 2 are within the limit which is 0 - 3, they will be added to the neighbor_index_list as:

(0,2) and (2,2) the value of these 2 indexes in grid are 3 and 8 respectively which is above and below the given value 3 (1,2)

"""

#get the indexes left and right of the given indexes

  for ncolumn in neighboring_column: #iterate through neighboring_column

#check if it is within the limit index, for the given grid since size of column is 5 it should be 0 - 4

    if(ncolumn >= 0 and ncolumn < column_size):

      neighbor_index_list.append((row, ncolumn))

#if within the limit add indexes as a tuple to the neighbor_index_list, notice that the given row is used

 

"""

for example : row = 1 and column = 2, the value of this indexes in the grid is grid[1][2] = 3

neighboring_column= [1, 3]

since 1 and 3 are within the limit which is 0 - 4, they will be added to the neighbor_index_list as:

(1,1) and (1,3) the value of these 2 indexes in grid are 2 and 0 respectively which is left and right of the given value 3 (1,2)

"""

 

  highest_neighbor = 0 #initialize highest_neighbor to zero

  for index in neighbor_index_list: #iterate through neighbor_index_list to get the neighboring indexes

    grid_value = grid[index[0]][index[1]] #get the value from the grid

    if grid_value > highest_neighbor: #if the grid_value of the neighboring indexes is greater than the current highest_neighbor

      highest_neighbor = grid_value # assign grid_value as the highest_neighbor

 

  return highest_neighbor #return the result

 

#to test the function

result = get_highest_neighbor(grid, 1, 2)

print(result)

result = get_highest_neighbor(grid, 1, 1)

print(result)

result = get_highest_neighbor(grid, 2, 3)

print(result)