Question: PYTHON Write a function valid_path(n, path) that takes in the size of the grid and a travel path, and returns True if the path is

PYTHON

Write a function valid_path(n, path) that takes in the size of the grid and a travel path, and returns True if the path is valid and False if the path is invalid.

A path is only valid when Eve ends at the goal coordinate (N1,N1) and the path conists of only legal moves between coordinates. Remember that coordinates must be inside the grid and legal moves are 1 unit: Right (East), Down (South), Diagonal-Right-Down (South-East).

The parameters to this function are as follows:

  • N is the dimension of the grid. For example, if N = 2, then the size of the grid is 22 with a goal at coordinate (1,1).
  • path is a list of coordinates represented in tuples, starting with (0, 0).

Input Assumptions:

  • You can assume that the input arguments are syntactically correct given the definitions, and will always be non-empty.
  • The path will always begin at the entrance coordinate (0,0), but it may not necessarily end at the goal node.

Here are example calls to your function:

valid_path(2, [(0, 0), (1, 1)])
>>> True
valid_path(4, [(0,0),(0,1),(0,2),(1,2),(2,3),(3,3)])
>>> True
valid_path(4, [(0,0),(1,0),(0,0),(0,1),(0,2),(1,2),(2,3),(3,3)])
>>> False
valid_path(4, [(0,0),(0,1),(0,2)])
>>> False

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!