Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import math import random import string import collections import datetime import re import time import copy # YOUR CODE BELOW... def loadDataIntoDictionary(fn) : your code

import math import random import string import collections import datetime import re import time import copy # YOUR CODE BELOW... def loadDataIntoDictionary(fn) : your code here... # end def def computeDistance(table, cityFrom, cityTo) : your code here... # end def def main( ) : table = loadDataIntoDictionary("cities.info") computeDistance(table, "toronto", "stuttgart") computeDistance(table, "London", "Zurich") computeDistance(table, "pARis", "VIENNA") computeDistance(table, "toronto", "TORONTO") computeDistance(table, "toronto", "Seoul") computeDistance(table, "tokyo", "San francisco") # end main( ) if __name__ == "__main__" : main( ) The OUTPUT should be EXACTLY as displayed below: 12 city records loaded... distance from Toronto, Canada to Stuttgart, Germany is: 6378.285 km ERROR... one or more city names NOT found! distance from Paris, France to Vienna, Austria is: 916.164 km distance from Toronto, Canada to Toronto, Canada is: 0.000 km ERROR... one or more city names NOT found! distance from Tokyo, Japan to San Francisco, United States is: 6701.629 km 
PART A: Write the code for a Python function with the following prototype: def loadDataIntoDictionary(fn) :  This function accepts the name of a file as a parameter 'fn' and loads the data in the file into a dictionary where the city is the key and the country and distance are stored as values. How the values are stored for each key will be up to you! This function displays the number of records loaded from the file as: n city records loaded... (where n is the number of records in the file) and then returns the loaded dictionary. If the file is not found, then the error message: ERROR... file data could not be loaded is displayed and the program terminates! NOTE: You MUST use Python exception handling to manage this error and program functionality. The correct file WILL BE available on the server when submitting your solution. PART B: Write the code for a Python function with the following prototype: def computeDistance(table, cityFrom, cityTo) :  This function accepts 3 parameters: 1. A dictionary of cities, countries, and distances created from PART A named 'table', 2. The name of a city to compute the distance from 'cityFrom', 3. The name of a city to compute the distance to 'cityTo', and computes and displays the distance in km from 'cityFrom' to 'cityTo'. The case of the letters in the parameter for each city name should not matter. If either city name is NOT found in the 'table' dictionary, then an error message: ERROR... one or more city names NOT found! is displayed and the function ends. For example, if the dictionary in the file "cities.info" (above) were loaded into 'table', then the following function calls: computeDistance(table, "toronto", "stuttgart") # would display: distance from Toronto, Canada to Stuttgart, Germany is: 6378.285 km computeDistance(table, "London", "Zurich") # would display: ERROR... one or more city names NOT found! computeDistance(table, "pARis", "VIENNA") # would display: distance from Paris, France to Vienna, Austria is: 916.164 km (6917.006 - 6000.842) NOTE: The larger distance to the city of Toronto MUST first be computed in order to get the correct value for distance. 

please do not make changes in main

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

Information Modeling And Relational Databases

Authors: Terry Halpin, Tony Morgan

2nd Edition

0123735688, 978-0123735683

More Books

Students also viewed these Databases questions