Skip to content

Instantly share code, notes, and snippets.

@adrianshort
Created May 26, 2014 13:45
Show Gist options
  • Save adrianshort/9ec176bfdeff667e02db to your computer and use it in GitHub Desktop.
Save adrianshort/9ec176bfdeff667e02db to your computer and use it in GitHub Desktop.
# D'Hondt method calculations
# https://en.wikipedia.org/wiki/D'Hondt_method
# By Adrian Short (@adrianshort) 26 May 2014
# European Parliament election, London region, 22 May 2014
@parties = {
'4 Freedoms Party (UK EPP)' => 28014,
'An Independence from Europe' => 26675,
'Animal Welfare Party' => 21092,
'British National Party' => 19246,
'Christian Peoples Alliance' => 23702,
'Communities United Party' => 6951,
'Conservative Party' => 495639,
'English Democrats' => 10142,
'Europeans Party' => 10712,
'Green Party' => 196419,
'Harmony Party' => 1985,
'Labour Party' => 806959,
'Liberal Democrats' => 148013,
'National Health Action Party' => 23253,
'National Liberal Party True Liberalism' => 6736,
'NO2EU' => 3804,
'UK Independence Party (UKIP)' => 371133
}
@seats = 8 # Total number of seats to be elected in this district
@averages = []
@parties.each do |party|
(1..@seats).each do |denominator|
@averages << [ party[0], party[1] / denominator ]
end
end
@winners = @averages.sort { |a,b| b[1] <=> a[1] }[0..@seats - 1]
puts "Place. Party"
(1..@seats).each do |place|
puts "%d. %s" % [ place, @winners[place - 1][0]]
end
@adrianshort
Copy link
Author

And the output:

Place. Party

  1. Labour Party
  2. Conservative Party
  3. Labour Party
  4. UK Independence Party (UKIP)
  5. Labour Party
  6. Conservative Party
  7. Labour Party
  8. Green Party

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment