The Problem

Here is a dataset of some 26000 cities of the world including their latitude and longitude. This is the data file that the tests will use. Write a program called gc.py that reads in a data file using the csv module. It should store in a dictionary the tuple of floats: latitude and longitude indexed by the tuple of ASCII city name and country. It will then take pairs of cities supplied on stdin and write out the distance in the same format as given in the test data. Note that on assignment 7 the test data has nonascii characters in it. This is a common thing. So when you open the file for reading use:

open("worldCities.csv", 'r', encoding="utf-8")
To change how character codes in the file are decoded to UTF-8.

The Problem Details

Write a function called getCity(dictionary). It should read in from stdin a comma separated city name and country like:
Moscow, United States

It will strip the white space from around both city and country. If the (city, country) is not in the dictionary it should inform the user of that fact and read another comma separated pair. The message it uses for the error is in the test output. It either returns the tuple (city, country) if it finds it or the value None if the user has used one of the strings: quit, stop, exit, or bye. You could test them individually or use a regular expression.

The function main will create the data base dictionary described above from the csv file (note the file has a header). Afterwards, it will go into a loop reading two cities using getCity described above. If either returns None the program quits. The program then computes the spherical great circle distance in miles between the given cities using the latitude and longitude given in the data file. The formula you should use is the haversine formula as described in the Wikipedia: Great Circle Distance page under Computational formulas. Assume the radius of a spherical Earth is 3959 miles. The output is rounded to 1 decimal place (use the round() function). I will take off if the output does not match character for character!

Given the test data:

Moscow, United States
Moscow, Russia
Moscow, United States
Spokane, United States
bugtussle, arkansas
smallville, USA
Sydney, Australia
smallerville, US
Spokane, United States
Perth, Australia
Spokane, United States
Moscow, United States
Hilo, United States
Spokane, United States
Hilo, United States
Durban, South Africa
Moscow, United States
Hilo, United States
Hilo, United States
bye

The output should be:

The great circle distance from Moscow, United States to Moscow, Russia is 5204.3 mi
The great circle distance from Moscow, United States to Spokane, United States is 67.8 mi
Cound not find city ('bugtussle', 'arkansas') in database
Cound not find city ('smallville', 'USA') in database
Cound not find city ('smallerville', 'US') in database
The great circle distance from Sydney, Australia to Spokane, United States is 7957.4 mi
The great circle distance from Perth, Australia to Spokane, United States is 9474.6 mi
The great circle distance from Moscow, United States to Hilo, United States is 2841.8 mi
The great circle distance from Spokane, United States to Hilo, United States is 2854.9 mi
The great circle distance from Durban, South Africa to Moscow, United States is 10370.4 mi
The great circle distance from Hilo, United States to Hilo, United States is 0.0 mi

Testing

Your object will be in a file called gc.py (note all lowercase). I will run your program in a directory containing the world city file named as above. I will use the line:

gc.py < testdata.txt

Submission

Homework will be submitted as an uncompressed tar file that contains no subdirectories. The tar file is submitted to the class submission page. You can submit as many times as you like. The LAST file you submit BEFORE the deadline will be the one graded. Absolutely, no late papers. For all submissions you will receive email at your uidaho address showing how your file performed on the pre-grade tests. The grading program will use more extensive tests, so thoroughly test your program with inputs of your own. Your code should compile and run without runtime errors such as seg faults or Python errors. If it doesn't it is considered nearly ungradable.

If you have tests you really think are important or just cool please send them to me and I will consider adding them to the test suite.