Question: In this exercise you will build a program that provides distance information for a road trip between European cities. Your program will read distance information
In this exercise you will build a program that provides distance information for a road trip between European cities. Your program will read distance information for a small collection of pairs of cities. Then it will allow the user to select a starting city and a destination city, and it will display the distance between the two cities. It may be that there is no direct road between the two cities in which case the program will report this fact.
Specifications: Here are the things you need to do:
Write a fruitful function named read_distances(file_name) that reads the road distance information from Distances.csv. Each line in the file contains the following: city1, city2, distance. As your function reads the data from each line it must create a sorted tuple containing the data. In your sorted tuple, the cities must be sorted alphabetically so that city1 < city2. Your program must create a set called distances and each sorted tuple must be added to the set. When the input phase is complete your distances set should contain a collection of distance information with no duplicates. (And you didnt need to write any code to discard any possible duplicates.) Your function must return the distances set to its caller.
Write a function called display_distances(distances) that prints the contents of the distances set in a nicely formatted manner.
Write a fruitful function called distances_to_kilometers(distances). Using the distances set, determine the number of unique cities are in the collection. (You can use a set to accomplish this.) Call this number n. Create an n x n matrix (this is called an adjacency matrix; you should name this matrix kilometers) containing one row for each city and one column for each city. Insert the distance between the row city and the column city. Note that each road is two-way, so youll need to insert the same distance for both row, column pairs and column, row pairs. This will create what is known as a symmetric matrix. Your function must return the kilometers matrix to its caller.
Write a function called display_kilometers(kilometers) that prints the contents of kilometers in a nicely formatted manner.
Create a fruitful function how_far(city1, city2) that accepts two city names and returns the road distance between the two cities by looking up the pair in the kilometers matrix. Note that if there is no direct road connecting city1 with city2 then how_far() should yield -1. Note too that the distance between a city and itself should yield zero. Note that how_far(city1,city2) should yield the same distance as how_far(city2,city1). If either city1 or city2 do not exist in the database, then how_far() should yield -2.
Build a main driver that:
Calls/uses the functions created in steps 1-4.
Uses your how_far() function to determine the number of kilometers between the following city pairs:
Paris Paris
Rome Paris
Brussels Geneva
Florence Rome
Naples Palermo
Frankfurt Catania
Nowhere Somewhere
In a few sentences describe how you could extend this program so that if there is no direct road between two cities, but they are connected via a collection of intermediate cities how the program might find the shortest path between the cities.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
