Skip to content

Instantly share code, notes, and snippets.

@joanfont
Created September 30, 2015 21:48
Show Gist options
  • Save joanfont/8a1c4b3b8122439d95cd to your computer and use it in GitHub Desktop.
Save joanfont/8a1c4b3b8122439d95cd to your computer and use it in GitHub Desktop.
info = {
'seats' => 85,
'votes' => {
'psc' => 420623,
'jxsi' => 1107398,
'cup' => 254246,
'cs' => 579850,
'csqep' => 311612,
'pp' => 272280
}
}
def get_most_voted_party_name(votes)
return votes.sort_by{ |key, value| value }.reverse.first[0]
end
votes = info['votes']
parties = votes.keys
seats = info['seats']
deputies = Hash.new(0)
party_divisors = Hash.new(1)
seats.times do |i|
divisions = Hash.new(0)
parties.each do |party|
divisions[party] = votes[party] / party_divisors[party]
end
most_voted_party_name = get_most_voted_party_name(divisions)
deputies[most_voted_party_name] += 1
party_divisors[most_voted_party_name] += 1
end
deputies_sorted = deputies.sort_by { |key, value| value}.reverse
deputies_sorted.each do |party, party_seats|
puts "#{party} : #{party_seats}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment