Short Exercises #3

Due: Sunday, Oct 25 at 9pm CDT

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

In some of these exercises, we will be using the candidate data discussed in the Module 3 videos. 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]: []
    

Building dictionaries

  1. Complete the function construct_dict_from_lists, which takes a list of keys and a list of values and constructs a dictionary that maps the ith key in the list of keys to the ith value in the list of values. You may assume that the lists will be the same length. Here are a couple of sample calls to this function:

    In [4]: se3.construct_dict_from_lists(["x", "y", "z"], [10, 20, 30])
    Out[4]: {'x': 10, 'y': 20, 'z': 30}
    
    In [5]: se3.construct_dict_from_lists([], [])
    Out[5]: {}
    
  2. Complete the function construct_homestate_dict, which takes a list of candidates and construct a dictionary that maps candidate IDs to the home states of the candidates. Here is 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_unsuccessful_fund_raisers, which takes a dictionary that maps candidate IDs to donations counts and a threshold and computes a list of the IDs of the candidates who received strictly less than the threshold number of contributions. For example,

    In [8]: dc = {"C00002600": 529,
       ...:       "C00012229": 749,
       ...:       "C00013128": 460}
    
    In [9]: se3.find_unsuccessful_fund_raisers(dc, 500)
    Out[9]: ['C00013128']
    
    In [10]: se3.find_unsuccessful_fund_raisers(dc, 600)
    Out[10]: ['C00002600', 'C00013128']
    
    In [11]: se3.find_unsuccessful_fund_raisers(dc, 400)
    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 (d["UT"]) is a list with only candidate, where as the value associated with Alabama (d["AL"]) is a list of with five candidates.

Testing and Submitting your Solutions

Like the previous short exercises, you will need to pull some instructor files to your repository, and then add your code to one of those files. You can find detailed instructions on how to do this in our Coursework Basics page. For instructions on how to test your code, please see our Testing Your Code

Once you’ve completed the exercises, you must submit your work through Gradescope (linked from our Canvas site). In the “Short Exercises #3” assignment, simply upload file se3.py (do not upload any other file!). Please note:

  • You are allowed to make as many submissions as you want before the deadline.

  • There are no extensions for the short exercises. The two free extensions you get for the programming assignments cannot be applied towards the short exercises. Please note that, if you need an extension due to extraordinary circumstances, you should alert us via a private message on Piazza.

  • Your score on the short exercises is determined solely based on the automated tests, but we may adjust your score if you attempt to pass tests by rote (e.g., by writing code that hard-codes the expected output for each possible test input).

  • Gradescope will report the test score it obtains when running your code. If there is a discrepancy between the score you get when running our grader script, and the score reported by Gradescope, please let us know so we can take a look at it.