problem23 spring

.pdf

School

Georgia Institute Of Technology *

*We aren’t endorsed by this school

Course

6040

Subject

Computer Science

Date

Dec 6, 2023

Type

pdf

Pages

41

Uploaded by ChefStraw5566

Report
11/28/23, 8:39 PM problem file:///Users/dannie/Downloads/pmt1-sample-solutions-su21/problem23-sample-solutions.html 1/41 Midterm 1: How partisan is the US Congress? Version 1.1b (Simplified sample solution for Exercise 6) This problem is about basic data processing using Python. It exercises your fundamental knowledge of Python data structures, such as lists, dictionaries, and strings. It has seven exercises, numbered 0-6. Each exercise builds on the previous one. However, they may be completed independently. That is, if you can't complete an exercise, we provide some code that can run to load precomputed results for the next exercise. That way, you can keep moving even if you get stuck. Pro-tips. If your program behavior seem strange, try resetting the kernel and rerunning everything. If you mess up this notebook or just want to start from scratch, save copies of all your partial responses and use Actions Reset Assignment to get a fresh, original copy of this notebook. (Resetting will wipe out any answers you've written so far, so be sure to stash those somewhere safe if you intend to keep or reuse them!) If you generate excessive output (e.g., from an ill-placed print statement) that causes the notebook to load slowly or not at all, use Actions Clear Notebook Output to get a clean copy. The clean copy will retain your code but remove any generated output. However , it will also rename the notebook to clean.xxx.ipynb . Since the autograder expects a notebook file with the original name, you'll need to rename the clean notebook accordingly. Good luck! Background The United States Congress is the part of the US government that makes laws for the entire country. It is dominated by two rival political parties, the Democrats and the Republicans. You would expect that these parties oppose each other on most issues but occasionally agree. Some have conjectured that, over time, the two parties agree less and less, which would reflect a perceived growing ideological or political divide in the US. But is that the real trend? In this problem, you'll explore this question using data collected by ProPublica (https://www.propublica.org/), a nonprofit investigative media organization. Setup and data loading Run the code cells below to load the data. This code will hold the data in two variables, one named votes and another named positions .
11/28/23, 8:39 PM problem file:///Users/dannie/Downloads/pmt1-sample-solutions-su21/problem23-sample-solutions.html 2/41 In [1]: import sys print(f"* Python version: {sys.version} ") from testing_tools import load_json, save_json, load_pickle, save_pick le votes = load_json("votes.json") vote_positions = [p for p in load_json("positions.json") if p['positio ns']] print(" \n ==> Data loading complete.") ### BEGIN HIDDEN TESTS % load_ext autoreload % autoreload 2 ### END HIDDEN TESTS Part 0: Analyzing vote results The Congress votes on various things, like new laws or political nominations. The results of these votes are stored in the votes variable loaded above. Let's look at it. First, note that votes is a list: In [2]: print(type(votes)) print("Length:", len(votes)) Vote results. What is votes a list of? Each element is one vote result. Let's look at the first entry. * Python version: 3.7.5 (default, Dec 18 2019, 06:24:58) [GCC 5.5.0 20171010] * JSON module version: 2.0.9 './resource/asnlib/publicdata/votes.json': 28596 './resource/asnlib/publicdata/positions.json': 279 ==> Data loading complete. <class 'list'> Length: 28596
11/28/23, 8:39 PM problem file:///Users/dannie/Downloads/pmt1-sample-solutions-su21/problem23-sample-solutions.html 3/41 In [3]: from testing_tools import inspect_data inspect_data(votes[0]) # View the first element of the list, `votes`
11/28/23, 8:39 PM problem file:///Users/dannie/Downloads/pmt1-sample-solutions-su21/problem23-sample-solutions.html 4/41 { "congress": 106, "chamber": "Senate", "session": 1, "roll_call": 374, "source": "https://www.senate.gov/legislative/LIS/roll_call_votes/ vote1061/vote_106_1_00374.xml", "url": "https://www.senate.gov/legislative/LIS/roll_call_lists/rol l_call_vote_cfm.cfm?congress=106&session=1&vote=00374", "vote_uri": "https://api.propublica.org/congress/v1/106/senate/ses sions/1/votes/374.json", "bill": { "bill_id": "h.r.3194-106", "number": "H.R..3194", "sponsor_id": null, "api_uri": null, "title": null, "latest_action": null }, "question": "On the Conference Report", "question_text": "", "description": "H.R.3194 Conference report; Consolidated Appropria tions Act, 2000", "vote_type": "1/2", "date": "1999-11-19", "time": "17:45:00", "result": "Agreed to", "democratic": { "yes": 32, "no": 12, "present": 0, "not_voting": 1, "majority_position": "Yes" }, "republican": { "yes": 42, "no": 12, "present": 0, "not_voting": 1, "majority_position": "Yes" }, "independent": { "yes": 0, "no": 0, "present": 0, "not_voting": 0 }, "total": { "yes": 74, "no": 24, "present": 0, "not_voting": 2 } }
11/28/23, 8:39 PM problem file:///Users/dannie/Downloads/pmt1-sample-solutions-su21/problem23-sample-solutions.html 5/41 Observation 0. This first entry of the list is a dictionary, and the data structure is nested even more. For instance, the "bill" key has another dictionary as its value. Remember that what you see above is votes[0] , the first entry of the votes list. For the rest of this problem, you may assume that all other entries of votes have the same keys and nesting structure. Exercise 0 (2 points). Let's pick out a subset of the data to analyze. Complete the function below, filter_votes(votes) . The input, votes , is a list of vote results like the one above. Your function should return a copy of this list, keeping only vote results meeting the following criteria: 1. The 'vote_type' is one of the following: "1/2" , "YEA-AND-NAY" , "RECORDED VOTE" 2. Notice that the 'total' key is a dictionary. Retain only the vote results where this dictionary has all of the fields 'yes' , 'no' , 'present' , and 'not_voting' . Your copy should include vote results in the same order as the input list. Note 0: The reason for Condition 2 above is that for some vote results, the 'total' dictionary does not have those fields. For an example, see votes[18319] . You would not include that vote result in your output. Note 1: The test cell does not use real vote results, but randomly generated synthetic ones. Your solution should not depend on the presence of any specific keys other than the ones you need for filtering, namely, 'vote_type' and 'total' . As an example, suppose V is the following vote results list (only the salient keys are included): V = [ {'vote_type': "1/2", 'total': {'yes': 5, 'no': 8, 'present': 0, 'not_v oting': 2}, ...}, {'vote_type': "RECORDED VOTE", 'total': {'yes': 12, 'present': 2, 'not _voting': 1}, ...}, {'vote_type': "3/5", 'total': {'yes': 50, 'no': 14, 'present': 0, 'not _voting': 0}, ...}, {'vote_type': "YEA-AND-NAY", 'total': {'yes': 25, 'no': 3, 'present': 3, 'not_voting': 0}, ...} ] Then running filter_votes(V) would return the following new list: [ {'vote_type': "1/2", 'total': {'yes': 5, 'no': 8, 'present': 0, 'not_votin g': 2}, ...}, {'vote_type': "YEA-AND-NAY", 'total': {'yes': 25, 'no': 3, 'present': 3, 'not_voting': 0}, ...} ] In this case, V[1] is omitted because its 'total' key is missing the 'no' key; and V[2] is omitted because the 'vote_type' is not one of "1/2" , "YEA-AND-NAY" , or "RECORDED VOTE" .
11/28/23, 8:39 PM problem file:///Users/dannie/Downloads/pmt1-sample-solutions-su21/problem23-sample-solutions.html 6/41 In [4]: def filter_votes(votes): assert isinstance(votes, list) and len(votes) >= 1 assert isinstance(votes[0], dict) ### BEGIN SOLUTION def matches(v): return (v["vote_type"] in {"1/2", "YEA-AND-NAY", "RECORDED VOT E"}) \ and (set(v["total"].keys()) == {"yes", "no", "present", "not_voting"}) return [v for v in votes if matches(v)] ### END SOLUTION In [5]: # Demo cell (feel free to use and edit for debugging) V = [ {'vote_type': "1/2", 'total': {'yes': 5, 'no': 8, 'present': 0, 'not_voting': 2}}, {'vote_type': "RECORDED VOTE", 'total': {'yes': 12, 'present': 2, 'not_voting': 1}}, {'vote_type': "3/5", 'total': {'yes': 50, 'no': 14, 'present': 0, 'not_voting': 0}}, {'vote_type': "YEA-AND-NAY", 'total': {'yes': 25, 'no': 3, 'pres ent': 3, 'not_voting': 0}} ] inspect_data(filter_votes(V)) print(len(filter_votes(votes))) [ { "vote_type": "1/2", "total": { "yes": 5, "no": 8, "present": 0, "not_voting": 2 } }, { "vote_type": "YEA-AND-NAY", "total": { "yes": 25, "no": 3, "present": 3, "not_voting": 0 } } ] 22178
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help