The United States House of Representatives consists of 435 voting members.

representatives
Seal of the House of Representatives

Each voting member is elected in a congressional district that is established on the basis of data from the census. On the basis of the decennial census it is determined how many representatives each state will receive in the House of Representatives (in the census of 2000 any seat represents an average of 646,952 inhabitants). Each state gets at least one seat. Every ten years, the states revise the number of congressional districts and the corresponding borders to reach conformity with the number of delegates they can send to the House of Representatives. One member is appointed for a period of two years. Furthermore, the House has six non-voting members from Guam, American Samoa, the Northern Mariana Islands, the Virgin Islands, Puerto Rico and the District of Columbia.

Since 1940, the number of representatives per state is determined using the Huntington-Hill method. This method was developed by Edward Huntington and Joseph Hill. In this method, the percentage differences between the sizes of the districts are minimized. The method begins by allocating one seat to each state. Then, the next step is repeated until there are no more seats left:

  1. For each state, the geometric average is calculated of the ratio of the population to the current number of seats and the ratio of the population to the number of seats if the next seat would go to that state. This average is given by the following formula: \[\frac{p}{\sqrt{n(n+1)}}\] where $$p$$ is the population and $$n$$ the current number of seats.

  2. The subsequent seat goes to the state for which the average is the highest.

At the first census in 1790 there were only 15 states, and there were only 105 seats in the House of Representatives. At that time the Huntington-Hill method was not used, but if that would have been the case, the distribution would have looked like this.

State Inhabitants Seats
Connecticut 236841 7
Delaware 55540 2
Georgia 70835 2
Kentucky 68705 2
Maryland 278514 8
Massachusetts 475327 14
New Hampshire 141822 4
New Jersey 179570 5
New York 331589 10
North Carolina 353523 10
Pennsylvania 432879 12
Rhode Island 68446 2
South Carolina 206236 6
Vermont 85533 3
Virginia 630560 18

Assignment

The file uscensus2010.csv1 contains the results of the censuses in the United States since 1910. The file uscensus1790.csv2 contains the results of the census in the US in 1790. The first element of each row, is the state with which that row corresponds. For the header the first element is empty. The elements in the header indicate which column corresponds to which year.

Asked:

  1. Write a function census to which the name of a file must be passed. This must be a CSV file with in the first column the names of the states and regions, and in subsequent columns numbers the censuses. The function also has a second optional argument that specifies which column in the CSV file will be used. By default, the second column is used, and the column parameter has the value 1. This function then returns a dictionary linking the names of the states with their population, the number of which is, of course, an integer.

  2. Write a function seatdistribution to which a dictionary must be passed as an argument. This dictionary is the same size as those returned by the census function. The function must return a new dictionary, which connects each state with the number of seats for that state. The function also has an optional second parameter total of which the default equals 435 and that shows how many seats are distributed in total.

Example

>>> census('uscensus2010.csv')
{'Mississippi': 1797114, 'Oklahoma': 1657155, ..., 'Maine': 742371}
>>> census('uscensus2010.csv', column=11)
{'Mississippi': 2967297, 'Oklahoma': 3751351, ..., 'Maine': 1328361}
>>> inhabitants = census('uscensus2010.csv', column=11)
>>> seatdistribution(inhabitants)
{'Mississippi': 4, 'Oklahoma': 5, ..., 'Maine': 2}