@extends('template')
@section('title')
Lab 11, Part 3: More dictionary practice
@stop
@section('content')
# Lab 11: Congress dictionary
In this part of lab, a congress dictionary is provided in JSON file format.
The congress dictionary contains every member of the United States Congress who served
at any point between January 1947 and Februrary 2014.
Notes:
+ The US Congress consists of two chambers: the House of Representatives (435 representatives) and the Senate (100 senators; 2 per state)
+ Congress data is provided by [fivethirtyeight](https://fivethirtyeight.com), if interested in exploring it on your own, you can download it [here](https://github.com/fivethirtyeight/data/tree/master/congress-age).
+ These data are behind the story [Both Republicans And Democrats Have an Age Problem](https://fivethirtyeight.com/features/both-republicans-and-democrats-have-an-age-problem/)
First, let's get familiar with the `congressDict` dictionary.
The **keys** are the names of the congress members, and the **values** are
dictionaries. For example, Loretta B. Sanchez served in the House of Representatives
for California from 1997 to 2017. She was first elected in 1996. Her name is the **key** in the
dictionary, and the **value** associated with her name is another dictionary.
Note that this example is formatted to make the key:value pairs easy to read.
Thonny, however, will display the key:value pairs on the same line.
```py
>> congressDict['Loretta B. Sanchez']
{'houseOrSenate': 'house',
'state': 'CA',
'party': 'D',
'birthday': '1960-01-07'}
```
Here are two more examples.
Joseph P. Kennedy has served as a House Representative for Massachusetts since 2013
and Elizabeth H. Dole served as a North Carolina Senator from 2003 to 2009.
```py
>>> congressDict['Joseph P. Kennedy']
{'houseOrSenate': 'house',
'state': 'MA',
'party': 'D',
'birthday': '1980-10-04'}
>>> congressDict['Elizabeth H. Dole']
{'houseOrSenate': 'senate',
'state': 'NC',
'party': 'R',
'birthday': '1936-07-29'}
```
## Task 1. `getCongressDict`
Partner B
Write a function called `getCongressDict` that takes the JSON file name as a parameter and returns the dictionary. You'll need this import statement at the top of your file so you can use the json
library.
```py
import json
```
Hint: you'll need to use json.load
to load in the json
file.
Here are some sample tests of the `getCongressDict` function:
```py
>>> getCongressDict('hello.json')
Oops, unable to read json file hello.json
>>> congressDict = getCongressDict('congress.json')
>>> len(congressDict)
3178
```
The remaining functions depend on the existence of the congressDict
dictionary.
## Task 2. `partyList`
Partner A
Write a function called `partyList` that takes the congress
dictionary and a party (eg, `'R'` for Republican, `'D'` for Democrat, or `'I'` for independent) and **returns** a list of all congress members that belong to the given party.
Here are some examples (you can scroll right in the window below to see all the names).
```py
>>> partyList(congressDict, 'I')
['Harry Flood Byrd', 'Eugene Joseph McCarthy', 'Hubert Horatio Humphrey', 'Henry Frazier Reams',
'obert Humphreys', 'Walter Frederick Mondale', 'James Lane Buckley', 'James M. Jeffords',
'Wendell Richard Anderson', 'Muriel Buck Humphrey', 'Joseph I. Lieberman', 'Bernard Sanders',
'Dean Barkley', 'Angus King']
>>> partyList(congressDict, 'D')
['Joseph Jefferson Mansfield', 'Robert Lee Doughton', 'Adolph Joachim Sabath', 'Sol Bloom',
'Schuyler Otis Bland', 'Louis Leon Ludlow', 'John Hosea Kerr', 'Robert Crosser',
'Clarence Frederick Lea', 'John Kee', 'Brent Spence', 'Mary Teresa Norton', ...]
```
## Task 3. `stateAndPartyList`
Partner B
This is very similar to Task 2 above. Write a function called `stateAndPartyList` that takes the congress dictionary and a party and a state and **returns** a list of all congress members that belong to the given party and the given state.
Here are some examples:
```py
>>> stateAndPartyList(congressDict, 'R', 'AK')
['Howard Wallace Pollock', 'Ted F. Stevens', 'Don E. Young', 'Frank H. Murkowski',
'Lisa A. Murkowski']
>>> stateAndPartyList(congressDict, 'I', 'VT')
['James M. Jeffords', 'Bernard Sanders']
>>> stateAndPartyList(congressDict, 'D', 'RI')
['Aime Joseph Forand', 'John Edward Fogarty', 'Theodore Francis Green', 'James Howard McGrath',
'Edward Laurence Leahy', 'John Orlando Pastore', 'Fernand Joseph St. Germain',
'Claiborne de Borda Pell', 'Robert Owens Tiernan', 'Edward Peter Beard', 'John F. Reed',
'Patrick Joseph Kennedy', 'Robert A. Weygand', 'James R. Langevin', 'Sheldon Whitehouse',
'David N. Cicilline']
```
## Task 4. `buildStateDict`
Partner A
You could imagine that it would be helpful to have this same data set
organized by state. In this task, you'll write a function called `buildStateDict` that takes the congress dictionary and builds a new dictionary. In this new dictionary, the **keys** are states (use the two-letter abbreviation for each state, e.g. 'MA' for Massachusetts), and **values** are **lists of tuples** of people representing that state in this format:
(name, party, houseOrSenate)
Notes about tuples:
+ tuples are immutable and ordered
+ placing parentheses around data makes it a tuple
```py
# tuple examples
myTuple = ('apple','banana','orange')
coreCSclasses = ('cs111','cs230','cs231','cs235','cs240','cs251')
ages = (17, 20, 21)
```
+ you can even omit the parentheses and just use commas to make a tuple
```py
fruitTuple = 'apple','banana','orange' # 3-element tuple
distances = 25, 93 # 2-element tuple
```
Here are some `buildStateDict` examples:
```py
>>> len(congressDict)
3178
>>> stateDict = buildStateDict(congressDict)
>>> len(stateDict)
50
>>> stateDict['AK']
[('Edward Lewis Bartlett', 'D', 'senate'), ('Ralph Julian Rivers', 'D', 'house'),
('Ernest Gruening', 'D', 'senate'), ('Howard Wallace Pollock', 'R', 'house'),
('Ted F. Stevens', 'R', 'senate'), ('Maurice Robert Gravel', 'D', 'senate'),
('Nicholas Joseph Begich', 'D', 'house'), ('Don E. Young', 'R', 'house'),
('Frank H. Murkowski', 'R', 'senate'), ('Lisa A. Murkowski', 'R', 'senate'),
('Mark Begich', 'D', 'senate')]
>>> stateDict['VT']
[('Charles Albert Plumley', 'R', 'house'), ('Ralph Edward Flanders', 'R', 'senate'),
('George David Aiken', 'R', 'senate'), ('Winston Lewis Prouty', 'R', 'senate'),
('William Henry Meyer', 'D', 'house'), ('Robert Theodore Stafford', 'R', 'senate'),
('Richard Walker Mallary', 'R', 'house'), ('James M. Jeffords', 'I', 'senate'),
('Patrick J. Leahy', 'D', 'senate'), ('Peter P. Smith', 'R', 'house'),
('Bernard Sanders', 'I', 'senate'), ('Peter Welch', 'D', 'house')]
>>> stateDict['CA']
[('Richard Joseph Welch', 'R', 'house'), ('Clarence Frederick Lea', 'D', 'house'),
('Franck Roberts Havenner', 'D', 'house'), ('Willis Winter Bradley', 'R', 'house'),
('Harry Richard Sheppard', 'D', 'house'), ('John Phillips', 'R', 'house'),
('Justin Leroy Johnson', 'R', 'house'), ('Bertrand Wesley Gearhart', 'R', 'house'),
...]
>>> len(stateDict['ME'])
29
>>> len(stateDict['NY'])
243
>>> len(stateDict['WI'])
58
```
## Task 5. `writeDictToJson`
Partner B
Write a function called `writeDictToJson` that takes two parameters: 1) the dictionary to be stored and 2) the json filename to store the dict. You'll need to use `json.dump`.
```py
>>> writeDictToJson(stateDict, 'congressStateDict.json')
# Check your lab11 folder for the new congressStateDict.json file
# You can drop it into Chrome to view the file (but not Safari)
```
## Task 6. `buildHouseSenateCountDict`
Partner A
Write a function called `buildHouseSenateCountDict` that takes one
parameter, the congress dictionary, and returns a new dictionary. The
new dictionary has two keys, `house` and `senate`. The values
associated with each of the keys is itself a dictionary, where the
keys of the subdictionary are the US states. The value for each state
is yet another dictionary, where the parties are the keys and the
counts of members of that party from that state are the values.
This is more easily seen in an example:
```py
>>> countDict = buildHouseSenateCountDict(congressDict)
```
Here is a glimpse of `countDict`:
```py
{ 'house': {'NY': {'D': 122, 'R': 105, 'AL': 2}
'TX': {'D': 93, 'R': 43},
'NC': {'D': 50, 'R': 24},
'IL': {'D': 65, 'R': 72},
'NJ': {'R': 40, 'D': 35},
'KY': {'R': 18, 'D': 23},
...
}
'senate':{'WY': {'R': 10, 'D': 4},
'IA': {'R': 6, 'D': 5},
'MD': {'R': 4, 'D': 7},
'NY': {'R': 6, 'D': 7, 'I': 1}
...
}
}
```
```py
>>> len(countDict)
2 # because only 2 keys, 'house' and 'senate'
>>> len(countDict['house'])
50 # 50 states
>>> len(countDict['senate'])
50 # 50 states
```
Some helpful questions to discuss with your partner as
you talk through this function. Let's use Loretta Sanchez as our example.
```py
>>> congressDict['Loretta B. Sanchez']
{'houseOrSenate': 'house',
'state': 'CA',
'party': 'D',
'birthday': '1960-01-07'}
```
+ What if Loretta is the first congress person we process?
How will we add them to the dictionary?
+ What if our dictionary already has 'CA' as a key in the 'house' sub-dictionary? How will you count Loretta as a 'D' democrat? What if 'CA' does not yet exist in the 'house' sub-dictionary?
@include('/labs/lab11/_toc')
@stop