Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a python script that satisfies the following specifications: It contains a function called read_map() that reads a map file ( metro_paths.txt ). This function

Write a python script that satisfies the following specifications:

  • It contains a function called read_map() that reads a map file (metro_paths.txt ). This function should take one argument (a path to the map file) and should return a dictionary where each key is a location and the corresponding value is a collection of destinations to which the location is directly linked by a path as specified in the map file. Note that paths are bidirectional (i.e., a path from point A to point B is also a path from point B to point A)--you will need to store the path in your dictionary in both directions. Also, note that you will need to strip newlines off of each line you read in from the map file.
    • For each location in the file, you will want to check whether that location is not already a key in the dictionary. If it is not, you should initialize it to an empty list or set (your choice). Then you can put things in it.
  • It contains a function find_route() that attempts to find a path between two locations. This function should take three arguments (the map dictionary; a starting location; and an ending location). It should return a list of locations that constitute a route from the starting location to the ending location such that for each consecutive pair of locations in the list there exists a path between the two locations. If no route is possible, it should return None.
  • It contains a function main() that reads the map data and finds a route between two locations. It should take three arguments (a path to the map file, a starting location, and an ending location) and should print the route it finds to stdout.
  • It takes three command-line arguments corresponding to the arguments of main().
  • The script should follow the style guide, including script and function docstrings, if __name__ == '__main__':, etc.

HINTS: Finding a route between two points

  • Create a list to serve as a queue of partial routes to investigate. Each item in the queue will be a list of locations that constitute a partial route.
  • Create a set of places you have "visited" so that you don't go in circles.
  • Add a list consisting of your starting destination to your queue.
  • While there are items in your queue:
    • Remove the first partial route from your queue (the pop() method is your friend) and store it in a variable.
    • In your map, look up the last location in the partial route.
    • For each other location accessible from the last location:
      • If you have not already visited the other location:
        • Add the other location to the set of places you have visited.
        • Copy the current partial route and add the other location to the end of the route.
        • If the other location is the destination, return that path.
        • Otherwise, add this path to the end of your queue.
  • If your loop ends and you have not returned a value, return None; there is no route between the two specified locations.

metro_paths.txt contains:

Addison Road Capitol Heights

Addison Road Morgan Boulevard

Anacostia Congress Heights

Anacostia Navy Yard-Ballpark

Archives Gallery Place

Archives L'Enfant Plaza

Arlington Cemetery Pentagon

Arlington Cemetery Rosslyn

Ballston-MU Virginia Square-GMU

Ballston-MU East Falls Church

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_2

Step: 3

blur-text-image_3

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

Database Development For Dummies

Authors: Allen G. Taylor

1st Edition

978-0764507526

More Books

Students also viewed these Databases questions

Question

1. Speak plainly and briefly, and avoid jargon.

Answered: 1 week ago

Question

What has been your desire for leadership in CVS Health?

Answered: 1 week ago

Question

Question 5) Let n = N and Y Answered: 1 week ago

Answered: 1 week ago

Question

=+Identify the key components of a strategic plan

Answered: 1 week ago