Last active
October 19, 2018 19:39
-
-
Save blackknight36/35ae922709ea414ad718288e1974d844 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
# Calculate the number of seats to apportion for each state in the union. | |
# This script is based on the algorithm described at https://www.census.gov/population/apportionment/about/computing.html | |
def largest_hash_key(hash) | |
hash.max_by{|k,v| v}[0] | |
end | |
def get_multiplier(n) | |
return 1/(n*(n-1)**0.5) | |
end | |
states = [ 'AK', 'AL', 'AR', 'AZ', 'CA', 'CO', 'CT', 'DC', 'DE', 'FL', 'GA', 'HI', 'IA', 'ID', 'IL', 'IN', 'KS', 'KY', 'LA', 'MA', 'MD', 'ME', 'MI', 'MN', 'MO', 'MS', 'MT', 'NC', 'ND', 'NE', 'NH', 'NJ', 'NM', 'NV', 'NY', 'OH', 'OK', 'OR', 'PA', 'PR', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VA', 'VT', 'WA', 'WI', 'WV', 'WY' ] | |
# population data copied from http://worldpopulationreview.com/states and Wikipedia | |
population = { | |
'AK' => 738068, | |
'AL' => 4888949, | |
'AR' => 3020327, | |
'AZ' => 7123898, | |
'CA' => 39776830, | |
'CO' => 5684203, | |
'CT' => 3588683, | |
'DC' => 703608, | |
'DE' => 971180, | |
'FL' => 21312211, | |
'GA' => 10545138, | |
'HI' => 1426393, | |
'IA' => 3160553, | |
'ID' => 1753860, | |
'IL' => 12768320, | |
'IN' => 6699629, | |
'KS' => 2918515, | |
'KY' => 4472265, | |
'LA' => 4682509, | |
'MA' => 6895917, | |
'MD' => 6079602, | |
'ME' => 1341582, | |
'MI' => 9991177, | |
'MN' => 5628162, | |
'MO' => 6135888, | |
'MS' => 2982785, | |
'MT' => 1062330, | |
'NC' => 10390149, | |
'ND' => 755238, | |
'NE' => 1932549, | |
'NH' => 1350575, | |
'NJ' => 9032872, | |
'NM' => 2090708, | |
'NV' => 3056824, | |
'NY' => 19862512, | |
'OH' => 11694664, | |
'OK' => 3940521, | |
'OR' => 4199563, | |
'PA' => 12823989, | |
'PR' => 3411307, | |
'RI' => 1061712, | |
'SC' => 5088916, | |
'SD' => 877790, | |
'TN' => 6782564, | |
'TX' => 28704330, | |
'UT' => 3159345, | |
'VT' => 623960, | |
'VA' => 8525660, | |
'WA' => 7530552, | |
'WV' => 1803077, | |
'WI' => 5818049, | |
'WY' => 573720, | |
} | |
seats_held = Hash.new(0) | |
pvalue = Hash.new(0) | |
states.each do |state| | |
pvalue[state] = population[state] * get_multiplier(1) | |
end | |
383.times do | |
# what state has largest pvalue | |
state = largest_hash_key(pvalue) | |
# give seat to that state | |
seats_held[state] += 1 | |
# update pvalue for that state | |
pvalue[state] = population[state] * get_multiplier(seats_held[state]) | |
end | |
# Start with 50 assigned | |
total = 52 | |
states.each do |state| | |
# Each state gets 1 seat by default | |
puts "#{state} seats: " + (seats_held[state] + 1).to_s | |
# Total seats | |
total += seats_held[state] | |
end | |
puts "Total: #{total}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment