From 1cc7182279ca21e0ac9510982000b427a804d16e Mon Sep 17 00:00:00 2001 From: Adrian Short Date: Mon, 26 May 2014 06:45:39 -0700 Subject: [PATCH] --- dhondt.rb | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 dhondt.rb diff --git a/dhondt.rb b/dhondt.rb new file mode 100644 index 0000000..23b2653 --- /dev/null +++ b/dhondt.rb @@ -0,0 +1,43 @@ +# 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