CS21 Lab 12: Zipcodes with Dictionaries

Optional lab.


This lab is completely optional and will not be graded. You may want to attempt this lab to gain additional practice with dictionaries. The basic goal of lab 12 is to revisit lab 08 and make a small modification to your ZIP Code search so that it uses a dictionary instead of binary search. You do not need to modify any other features of of the lab.

If you wish to make modifications to your lab 08, we suggest doing this in a separate folder. The following commands will create a copy of your 08 files in a new folder called 12.

cd ~/cs21/labs/
cp -r 08 12
cd 12
atom ./

Modifying find by ZIP

For this optional lab, you will only modify one of the menu options from lab 08; the find by ZIP Code option. Instead of using binary search on a list, you will use a key search on a dictionary.

ZIP Code database

Is 12345 a valid ZIP code? What is the ZIP code for Truth or Consequences, NM? Your assignment this week is to create a program zipcodes.py that allows a user to explore a ZIP code database. ZIP codes are used by the US Postal service to direct mail to cities more efficiently. An Online Interactive Viewer allows you to rapidly see how ZIP codes are distributed nationally (sadly, Alaska and Hawaii are omitted from the demo), and can help you find the names of places associated with any ZIP code.

The file /usr/local/doc/zipcodes.txt contains ZIP code data for most of the United States (don't copy this file to your 08 directory, just use "/usr/local/doc/zipcodes.txt" as the file name in your python program). Each line of the file contains the following fields separated by commas:

ZIP code
Latitude
Longitude
City name
County name
State
Population

The entry for Swarthmore is shown here:

19081,39.897562,-075.346584,Swarthmore,Delaware,PA,9907

For this lab, your program should prompt the user with a menu of 5 choices:

1. Find by ZIP code
2. Find by name
3. Map state
4. Map county
5. Quit

You program should ask the user for a valid choice and then perform the following queries listed below for each menu option, then return to the main menu again until the user selects option 5. Quit. This optional lab should only modify your program from lab08 by adjusting your solution for option 1.

Find by ZIP

For option 1 (Find a ZIP code) you must:

Some examples are shown below:

Welcome to the zipcode program!

Please select one of the following choices:

1. Find by ZIP code
2. Find by name
3. Map state
4. Map county
5. Quit

Choice? 1
Enter a 5-digit ZIP code: Corgis
Corgis is not a valid ZIP code. Try again.
Enter a 5-digit ZIP code: 19081

Swarthmore, PA, 19081
Delaware County
Population: 9907

1. Find by ZIP code
2. Find by name
3. Map state
4. Map county
5. Quit

Choice? 1
Enter a 5-digit ZIP code: 99709

Fairbanks, AK, 99709
Fairbanks North Star County
Population: 52316

1. Find by ZIP code
2. Find by name
3. Map state
4. Map county
5. Quit

Choice? 1
Enter a 5-digit ZIP code: 99999

No info on this ZIP exists.

1. Find by ZIP code
2. Find by name
3. Map state
4. Map county
5. Quit

To implement this feature you may want to write one new helper function that takes a list of ZIP Code objects as input and returns a newly created python dictionary with a ZIP Code string keys and ZIP Code object values.

Make a call to your helper function in main and save the dictionary as a variable that you then pass as an argument to your helper function for search by ZIP Code. You may then need to make some small changes to this function from lab 08 as it used a list and binary search before and now it will be using a dictionary and its lookup operations instead.

Other Features

You do not need to implement, or fix find by name, map state, or map county.

Hints and Tips

You should practice good top-down design, incrementally implement and test your solution, and document your code with comments.

The boundaries library has some helpful tools for managing the ZIP code data.

from boundaries import *

The first tool is a small ZipCode class. You can create a new ZipCode object by providing a ZIP, latitude, longitude, city name, county name, state name, and population to the ZipCode constructor. It is expected that latitude and longitude are floats, population is an int, and all other parameters, including ZIP codes are strings. The following ZipCode methods allow you to access the information of a single ZipCode object

lat = 39.897562
long = -075.346584
place = ZipCode("19081", lat, long, "Swarthmore", "Delaware", "PA" , 9907)
print(place.getZip())

To create a list of all the places from a file, you could use a sample function like the one below:

def readData(filename):
    """
    Read file of zip data, return list of
    zipcode objects
    """
    allzips = []
    f = open(filename)
    for line in f:
          line = line.strip().split(",")
          place = ZipCode(line[0], float(line[1]), float(line[2]),
           line[3], line[4], line[5], int(line[6]))
          allzips.append(place)
    f.close()
    return allzips
 place = allzips[10]
 print(place.getName())