You should save your program for this assignment in cs21/homework/08. A skeleton version of the program will appear in this directory when you run update21 in a terminal window. The program handin21 will only submit files in this directory. You may work with a partner on this assignment.
Some of the problems will have optional components that allow you to
further practice your skills in Python. Optional portions will not be
graded, but may be interesting for those wanting some extra challenges.
The file /usr/local/doc/zipcodes.txt contains the data you need to write your program. Each line of the file contains seven fields separated by commas representing the ZIP code, latitude, longitude, city name, county name, state, and population, respectively. The entry for Swarthmore is shown below:
The full file has entries for 41824 unique ZIP codes. You can open up the file in idle or any other text editor if you would like to look at the file in more detail. When you read in a line from this file you can use line.split(",") to separate the line into a list of strings.
Your program should prompt the user for two cities. The user can lookup a city by ZIP code or by a city name and its two letter state abbreviation. For each city, display the following information:
In addition to the information above, display the distance between the two cities the user typed.
A sample run is shown below:
This program creates a database of information about cities throughout the United States. It allows you to enter two cities (either by zip code or by city and state) and reports the distance between them. -------------------------------------------------------- CITY #1 Enter a zip code or city, state> Swarthmore, PA City, State, ZIP: Swarthmore, PA, 19081 County: Delaware Population: 6170 Lat / Long: 39.90 / -75.34 Closest big city: Philadelphia, PA (14.0 miles) CITY #2 Enter a zip code or city, state> 90210 City, State, ZIP: Beverly Hills, CA, 90210 County: Los Angeles Population: 33784 Lat / Long: 33.79 / -118.30 Closest big city: Lancaster, CA (0.0 miles) Distance between Swarthmore, PA and Beverly Hills, CA is 2391.6 miles Check more locations (yes/no)? yes -------------------------------------------------------- CITY #1 Enter a zip code or city, state> San Francisco, CA City, State, ZIP: San Francisco, CA, 94101 County: San Francisco Population: 776733 Lat / Long: 37.78 / -122.73 Closest big city: Daly City, CA (17.2 miles) CITY #2 Enter a zip code or city, state> Boston, MA City, State, ZIP: Boston, MA, 02101 County: Suffolk Population: 589141 Lat / Long: 42.37 / -71.03 Closest big city: Cambridge, MA (5.5 miles) Distance between San Francisco, CA and Boston, MA is 2712.4 miles Check more locations (yes/no)? no
If you cannot find an entry for a particular city or ZIP code, inform the user that you cannot find that location and prompt them to enter another location.
You are required to use at least one dictionary to store the records for each city. This dictionary must be indexed on ZIP code. For example, if the dictionary is called zipDict then zipDict['19081'] should return all the necessary information about Swarthmore, PA. You may use other dictionaries and lists as needed, but your implementation must include this specific dictionary.
How you design your program is very much up to you. You should use top down design to sketch out
a basic outline of your program on paper before you begin coding.
We have included some tips and problems to watch out for below.
Some large cities (Philadelphia, PA, for example) have multiple zip
codes. When asked to report a ZIP code for a city, you can decide how to
handle this. Possible options include reporting all ZIP codes for a
particular city, reporting the first ZIP code you find for that city,
reporting one example ZIP code and stating there are other possibilities,
or designing a method of your own choosing. You must report at least one
ZIP code for a city if it is in the list of known cities.
Be careful when reporting the closest city to a given city. Do not report
the same city. For example, the city with a population over 100,000
closest to Philadelphia, PA, should not be Philadelphia, PA, but some
other city. This is further complicated by the fact that many big cities
have multiple ZIP codes, when reporting the closest big city, ensure that
the city names are in fact different.
You should use the "great circle" distance formula to compute the distance between two cities. If the first city is at a geographic location (lat1, long1), and the second city is at (lat2, long2), then the distance between the two cities is given by the formula:
D = R acos( sin(lat1)sin(lat2) + cos(lat1)cos(lat2)cos(long2-long1))
Where R is the radius of the Earth (6371 km or 3963 miles), and acos is the inverse cosine. You can get this function by importing the math module in python. Note that each lat/long must be converted from degrees to radians before computing a sine or cosine. The formula for this conversion is
rad = deg*pi/180
This data file we have provided you is by no means complete. Some ZIP
codes were missing or did not have lat/long data and were removed. For
some cities, we did not have population data, so we set the population
arbitrarily to 0. If your favorite US city or hometown in the US is
missing and you know all the info (ZIP, city name, county name, state,
lat, long, and population), let us know and we will be happy to add a few cities, but we are not trying to maintain a comprehensive list.
Once you are satisfied with your program, hand it in by typing handin21 in a terminal window.