Given a list of the populations of each state, output, from greatest to least, the number of votes that state gets in the electoral college.
Input: The first number represents the total number of votes to distribute; it is followed by a list of and populations. In this example, abbreviations for states are used, but any name containing uppercase and lowercase letters could be used. You can take this in any format you like, as long as the only information contained is the state's abbreviation and its population.
Input may be taken as arguments to a function, or any way you want.
Example (possible) input: 538 [[CA 38000000], [NH 1300000] etc.]
Output: Output, in some format, the number of votes each state gets. Order the states from greatest to least. If two states have the same number of votes, order by whichever name would come first in a dictionary (which comes first alphabetically).
Before finding the number of votes, first check if there is a state named DC in the list of inputs, and if there is, give the state 3 votes, regardless of its population. Then, remove it from the list and assign the rest of the votes as though DC didn't exist.
The number of votes in the electoral college is defined to be the sum of the number of senators and representatives. Every state gets two senators, so subtract twice the number of states from the total (538, in the example input) to get the number of representatives. Assign every state one representative to start. Then, perform the following process:
Assign each state a number,
A
, defined to beP/sqrt(2)
whereP
is the population.Sort the states according to their values of
A
.Assign the first state (the one with the largest
A
) one more representative.Reassign values of
A
, asA = P/(sqrt(n)*sqrt(n + 1))
, wheren
is the current number of representatives assigned to the state.Go back to step 2. Repeat until all representatives have run out.
Example (possible) output: {CA: 518, NH: 20}
. The output doesn't have to be in this format, but must contain the same information.
Note that if it is not possible to assign votes legally because there are less than 3*(# of states)
votes, print whatever you want. You can crash, throw an error, etc.
Test cases:
538 [['CA' 38000000], ['NH' 1300000]] --> CA: 518, NH: 20
538 [['NH' 1300000], ['CA' 38000000]] --> CA: 518, NH: 20 (must be in order from greatest to least!)
538 [['DC' 1000000], ['RH' 1]] --> RH: 535, DC: 3
100 [['A', 12], ['B', 8], ['C', 3]] --> A: 51, B: 35, C: 14
100 [['A', 12], ['B', 8], ['C', 3], ['D', 0]]: --> [49, 34, 14, 3] (yes, even states with no population get votes)
2 [['A', 1]] --> aasdfksjd;gjhkasldfj2fkdhgas (possible output)
12 [['A', 1], ['B', 2], ['C', 3], ['D', 4]] --> A: 3, B: 3, C: 3, D: 3
42 [['K', 123], ['L', 456], ['M', 789]] --> M: 23, L: 14, K: 5
420 [['K', 123], ['L', 456], ['M', 789]] --> M: 241, L: 140, K: 39
135 [['C', 236841], ['D', 55540], ['G', 70835], ['K', 68705], ['M', 278514], ['Ms', 475327], ['Nh', 141822], ['Nj', 179570], ['Ny', 331589], ['Nc', 353523], ['P', 432879], ['R', 68446], ['Sc', 206236], ['Ve', 85533], ['Vi', 630560]] --> Vi: 20, Ms: 16, P: 14, Nc: 12, Ny: 12, M: 10, C: 9, Sc: 8, Nj: 7, Nh: 6, Ve: 5, D: 4, G: 4, K: 4, R: 4
A
, asA = P/(sqrt(n)*sqrt(n + 1))
, wheren
is the current number of members assigned to the state." should be changed to "Reassign values ofA
, asA = P/(sqrt(n)*sqrt(n + 1))
, wheren
is the current number of representatives assigned to the state.". That threw me off. \$\endgroup\$