Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help me with this homework assignment I'm struggling GIVEN CODE: from hw5_util import * # a function to print the grid def print_grid(grid): for

Please help me with this homework assignment I'm struggling

GIVEN CODE:

from hw5_util import *

# a function to print the grid

def print_grid(grid):

for row in grid:

for item in row:

print(item, end=' ')

print()

# a function to get neighbours of point marked by (row, col)

def get_neighbors(grid, rows, cols, row, col):

neighbors = []

if row - 1 >= 0:

neighbors.append((row-1, col))

if col -1 >= 0:

neighbors.append((row, col-1))

if row + 1

neighbors.append((row+1, col))

if col + 1

neighbors.append((row, col + 1))

return neighbors

# a function returns true if two points are adjacent

def is_adjacent(point_a, point_b):

if abs(point_a[0] - point_b[0]) == 1 and point_a[1] == point_b[1]:

return True

if abs(point_a[1] - point_b[1]) == 1 and point_a[0] == point_b[0]:

return True

return False

# a function which returns true if a point is valid point

def is_valid_point(point, rows, cols):

if 0

return True

return False

# a function which determines if the path is valid or not

def is_valid_path(path, rows, cols, index):

if not(is_valid_point(path[index], rows, cols)):

return False

if index == len(path) - 1:

return True

if is_adjacent(path[index], path[index + 1]):

return is_valid_path(path, rows, cols, index + 1)

else:

return False

# a function to calculate the total up or down elevation

def calculate_up_down(grid, path):

up = 0

down = 0

curr_loc = path[0]

for index in range(1, len(path)):

loc = path[index]

elevation = grid[loc[0]][loc[1]] - grid[curr_loc[0]][curr_loc[1]]

if elevation > 0:

up += elevation

elif elevation

down += abs(elevation)

curr_loc = path[index]

return up, down

# the main method

def main():

# read grid

read_grids()

grid_no = 0

while True:

try:

grid_no = int(input('Enter a grid index less than or equal to 3 (0 to end): '))

if grid_no == 0:

print('Exitting . . .')

exit(0)

if grid_no 3:

print('Please enter number from 1 to 3 only')

else:

break

except:

print('Invalid number!')

print()

choice = input('Should the grid be printed (Y or N): ')

grid = get_grid(grid_no)

if choice.lower() == 'y':

print('Grid', grid_no)

print_grid(grid)

rows = len(grid)

cols = len(grid[0])

print('Grid has {0} rows and {1} columns'.format(rows, cols))

start_locations = get_start_locations(grid_no)

for loacation in start_locations:

print('Neighbors of {0}: {1}'.format(loacation, get_neighbors(grid, rows, cols, loacation[0], loacation[1])))

path = get_path(grid_no)

if is_valid_path(path, rows, cols, 0):

print('Valid path')

up, down = calculate_up_down(grid, path)

print('Downward', down)

print('Upward', up)

else:

for index in range(1, len(path)):

if (not is_adjacent(path[index], path[index + 1])) or (not is_valid_point(path[index], rows, cols)) or (not is_valid_point(path[index], rows, cols)):

print('Path invalid step from {0} to {1}'.format(path[index], path[index + 1]))

break

main()

HERE'S THE ACTUAL PROBLEM:

image text in transcribed
You may assume without checking that the global maximum is unique. The main task of Part 2 is to find and output two paths from each start location for the grid. The first is the steepest path up, and the second is the most gradual path up. The steps on each path must be between neighboring locations as in Part 1. Also, on each path no steps to a location at the same height or lower are allowed, and the step size (the change in height) can be no more than a maximum step height (difference between heights at the new location and at the current location). Your code must ask the user for the value of this maximum step height. Next, determine for each path if it reaches the location of the global maximum height in the grid, a local maximum, or neither. The latter can happen at a location where the only upward steps are too high relative to the height at the current location. Of course, true hiking paths can go both up and down, but finding an "optimal path" in this more complicated situation requires much more sophisticated algorithms than we are ready to develop here. As an example of the required results, here is the same grid as above: grid = [[15, 16, 18, 19, 12, 11], [13, 19, 23, 21, 16, 12], [12, 15, 17, 19, 20, 10], [10, 14, 16, 13, 9, 6]] starting at location (3, 0) with a maximum height change of 4, the steepest path is (3, 0), (3, 1). (3, 2). (2, 2), (2, 3), (1, 3), (1, 2), while the most gradual path is (3, 0), (2, 0), (1, 0), (0, 0), (0, 1). (0, 2), (0, 3), (1, 3), (1, 2). Both reach the global maximum, and both avoid stepping to the global maximum the first time they are close because the step height is too large. Note that both the steepest and most gradual paths from location (3, 5) would end at the local maximum (2, 4). The steepest path would end after four steps (five locations on the path) and the most gradual would end after six steps (seven locations on the path). If the max step height were only 3, then both paths from (3, 5) would stop at location(3, 4) before any maximum is reached. Paths should be output with 5 locations per line, for example steepest path (3, 0) (2, 0) (1, 0) (0, 0) (0, 1) (0, 2) (0, 3) (1, 3) (1, 2) global maximum See the example output for further details. Finally, if requested by the user, output a grid - we'll call it the "path grid" - giving at each location the number of paths that include that location. This can be handled by forming a new list of lists, where each entry represents a count - initialized to 0. For each path and for each location (i, j) on the path, the appropriate count in the list of lists should be incremented. At the end, after all paths have been generated and added to the counts, output the grid. In this output, rather than printing a 0 for a locations that are not on any path, please output a ' . '; this will make the output clearer. See the example output. Notes 1. In deciding the choice of next locations on a path, if there is a tie, then pick the one that is earlier in the list produced by your get_nors function. For example starting at (0, 5) with elevation 11 in the above example grid, both (0, 4) and (1, 5) have elevation 12. In this case (0, 4) would be earlier in the get_nors list and therefore chosen as the next location on the path. 2. Please do not work on the path grid output - the last step - until you are sure you have everything else working. 3. Both the most gradual and steepest paths are examples of greedy algorithms where the best choice available is made at every step and never reconsidered. More sophisticated algorithms would consider some form of backtracking where decisions are undone and alternatives recon- sidered

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions

Question

=+How does the strategy meet the tests indicated here?

Answered: 1 week ago