Short Exercises #3

Due: Wednesday, Oct 19 at 4:30pm CT

The following short exercises are intended to help you practice some of the programming concepts introduced in the first three weeks of the quarter. These exercises should not take more than 1-2 hours in total to complete.

Fetching the instructor files

If this is your first time working through a set of Short Exercises, please work through Short Exercises #0 to set up your Short Exercises repository.

To get the files for this set of short exercises, first set the GITHUB_USERNAME environment variable by running the following command at the Linux command line (replacing replace_me with your GitHub username):

GITHUB_USERNAME=replace_me

Remember you can double-check whether the variable is properly set by running echo $GITHUB_USERNAME.

Then navigate to your Short Exercises repository and pull the new material:

cd ~/capp30121
cd short-exercises-$GITHUB_USERNAME
git pull upstream main

You will find the files you need in the se3 directory.

IMPORTANT: If you are unable to obtain the instructor files by running the commands above do not try to add the files in some other way. Doing so will likely prevent you from submitting your code. Instead, please seek assistance on Ed Discussion or at office hours.

Candidate data set

In some of these exercises, we will be using the candidate data discussed in lecture. Here is a sample candidate dictionary:

{'Candidate_ID': 'C00466698',
 'City': 'ANCHORAGE',
 'District': '00',
 'First': ' HARRY T JR',
 'Last': 'CRAWFORD',
 'Party': 'DEM',
 'State': 'AK',
 'Zipcode': '99504'}

The data we will be using is stored in two files tests/small_candidates.csv and tests/candidates.csv. We have provided a function, read_CSV_file in test_helpers.py to load the data from the file into a list of dictionaries for you.

Here are example uses of this function:

In [7]: import test_helpers

In [8]: small_candidates = test_helpers.read_CSV_file("tests/small_candidates.csv")

In [9]: candidates = test_helpers.read_CSV_file("tests/candidates.csv")

We will use the small_candidates in some of our examples below. Both small_candidates and candidates are used in the test code.

We encourage you to load the data and play around with it a bit before you start working on the exercises.

Exercises

Accessing Values in Dictionaries

  1. Complete the function find_candidates_from_city, which given a list of candidate dictionaries and an office location, represented as a tuple with the name of the city and the standard abbreviation for the state, returns a list of the candidate IDs for candidates whose headquarters are located in the specified location. Here are two sample uses of this function:

    In [1]: import se3
    
    In [2]: se3.find_candidates_from_city(small_candidates, ("BIRMINGHAM", "AL"))
    Out[2]: ['C00464040', 'C00460410', 'C00461038', 'C00471235', 'C00482091']
    
    In [3]: se3.find_candidates_from_city(small_candidates, ("NEWARK", "NJ"))
    Out[3]: []
    

    We specify both city and state, rather than just the city name, because some city names occur in multiple states (for example, Westminster, CA and Westminster, CO).

    Remark: This data set is inconsistent about capitalization (for example, one candidate is from “Loveland, CO”, and another is from “LOVELAND, CO”). You should not worry about this (that is, you should not do extra work to treat these two capitalizations as the same, and should instead should allow “Loveland” and “LOVELAND” to be considered as different cities). However, in the real world, data sets can be messy, and one often needs to spend time cleaning them up.

As usual, once you’ve written your code for this function, you should test it (with manual and automatic tests) before moving on. A reminder on how to test your code is on the Testing Your Code page. As you continue with these exercises, remember to test each function before moving onto the next.

Building dictionaries

  1. Complete the function construct_dict_from_lists, which takes a list of (key, index) pairs and a list of values and returns a dictionary. For each (key, index) pair, the dictionary should map that key to the value at the corresponding index in the list of values. Here are a couple of sample calls to this function:

    In [4]: se3.construct_dict_from_lists([("x", 2), ("y", 0), ("z", 2)], [0, 10, 20])
    Out[4]: {'x': 20, 'y': 0, 'z': 20}
    
    In [5]: se3.construct_dict_from_lists([], [])
    Out[5]: {}
    
  2. Complete the function construct_homestate_dict, which takes a list of candidates and constructs a dictionary that maps candidate IDs to the home states of the candidates. Here are two sample calls to this function:

    In [6]: se3.construct_homestate_dict(small_candidates)
    Out[6]:
    {'C00464040': 'AL',
     'C00460410': 'AL',
     'C00461038': 'AL',
     'C00471235': 'AL',
     'C00482091': 'AL',
     'C00068353': 'VT',
     'C00104752': 'UT',
     'C00091892': 'MS'}
    
    In [7]: se3.construct_homestate_dict([])
    Out[7]: {}
    

Iterating over dictionaries

  1. Complete the function find_successful_fund_raisers, which takes a dictionary that maps candidate IDs to donation counts and a threshold and computes a list of the IDs of the candidates who received at least the threshold number of contributions. For example,

    In [8]: dc = {"C00002600": 529,
       ...:       "C00012229": 749,
       ...:       "C00013128": 460}
    
    In [9]: se3.find_successful_fund_raisers(dc, 500)
    Out[9]: ['C00002600', 'C00012229']
    
    In [10]: se3.find_successful_fund_raisers(dc, 600)
    Out[10]: ['C00012229']
    
    In [11]: se3.find_successful_fund_raisers(dc, 800)
    Out[11]: []
    

Building more complex dictionaries

  1. Complete the function construct_cands_by_state, which takes a list of candidates, and constructs a dictionary that maps a state abbreviation to a list of the candidates from that state. Here’s a sample call to this function:

    In [19]: d = se3.construct_cands_by_state(small_candidates)
    
    In [20]: d["UT"]
    Out[20]:
    [{'Candidate_ID': 'C00104752',
      'City': 'SALT LAKE CITY',
      'District': '00',
      'First': ' ORRIN G',
      'Last': 'HATCH',
      'Party': 'REP',
      'State': 'UT',
      'Zipcode': '84101'}]
    
    In [21]: d["AL"]
    Out[21]:
    [{'Candidate_ID': 'C00464040',
      'City': 'BIRMINGHAM',
      'District': '07',
      'First': ' MARTHA RENEE',
      'Last': 'BOZEMAN',
      'Party': 'UNK',
      'State': 'AL',
      'Zipcode': '35201'},
     {'Candidate_ID': 'C00460410',
      'City': 'BIRMINGHAM',
      'District': '07',
      'First': ' EARL FREDERICK JR',
      'Last': 'HILLIARD',
      'Party': 'DEM',
      'State': 'AL',
      'Zipcode': '35202'},
     {'Candidate_ID': 'C00461038',
      'City': 'BIRMINGHAM',
      'District': '07',
      'First': ' SHEILA',
      'Last': 'SMOOT',
      'Party': 'DEM',
      'State': 'AL',
      'Zipcode': '35201'},
     {'Candidate_ID': 'C00471235',
      'City': 'BIRMINGHAM',
      'District': '07',
      'First': ' MICHELE',
      'Last': 'WALLER',
      'Party': 'REP',
      'State': 'AL',
      'Zipcode': '35202'},
     {'Candidate_ID': 'C00482091',
      'City': 'BIRMINGHAM',
      'District': '07',
      'First': ' ROBERT CHRISTOPHER',
      'Last': 'SALTER',
      'Party': 'REP',
      'State': 'AL',
      'Zipcode': '35206'}]
    

Notice that the value associated with Utah in d (that is, d["UT"]) is a list with only one candidate, whereas the value associated with Alabama (d["AL"]) is a list with five candidates.

Testing and Submitting your Solutions

Testing your solutions

Make sure to test your code as you go along; see the Testing Your Code page.

Submitting your work

Once you’ve completed the exercises, you must submit your work through Gradescope (linked from our Canvas site). Gradescope will fetch your files directly from your GitHub repository, so it is important that you remember to commit and push your work!

To submit your work, go to the “Gradescope” section on our Canvas site. Then, click on “Short Exercises #3”. Then, under “Repository”, make sure to select your uchicago-CAPP30121-aut-2022/short-exercises-$GITHUB_USERNAME.git repository. Under “Branch”, just select “main”.

Finally, click on “Upload”. An autograder will run, and will report back a score. Please note that this autograder runs the exact same tests (and the exact same grading script) described in Testing Your Code. If there is a discrepancy between the tests when you run them on your computer, and when you submit your code to Gradescope, please let us know.

Your ESNU score on this set of exercises will be determined solely on the basis of these automated tests:

Grade

Percent tests passed

Exemplary

at least 95%

Satisfactory

at least 75%

Needs Improvement

at least 55%

Ungradable

less than 55%

If there is a discrepancy between the tests when you run them on your computer, and when you submit your code to Gradescope, please let us know. Please remember that you can submit as many times as you want before the deadline. We will only look at your last submission, and the number of submissions you make has no bearing on your score.