Skip to content

Instantly share code, notes, and snippets.

@xbony2
Created June 9, 2019 13:29
Show Gist options
  • Save xbony2/6526334549d70c799175bbad8cca3498 to your computer and use it in GitHub Desktop.
Save xbony2/6526334549d70c799175bbad8cca3498 to your computer and use it in GitHub Desktop.
arr = [7600, 5900, 1400]
seats = 37
total = 0.0
arr.each { |e| total += e }
puts "Total: #{total}"
s_d = total / seats
m_d = s_d
puts "SD: #{s_d}"
puts
loop do
m_d -= 1 # Changed to 0.1 or lower if you get a divide by zero error
sum = 0
arr.each { |e| sum += (e / m_d).truncate}
break if sum == seats
end
puts "MD: #{m_d}"
puts
puts "State\tPopulation\tQ\tLQ\tMQ\tApportionment"
arr.each do |v|
s_q1 = v / s_d
s_q2 = v / m_d
puts "#{arr.index(v)}\t#{v}\t#{s_q1.truncate(3)}\t#{s_q1.truncate}\t#{s_q2.truncate(3)}\t#{s_q2.truncate}"
end
l_q_total = 0
arr.each { |e| l_q_total += (e / s_d).truncate}
app_total = 0
arr.each { |e| app_total += (e / m_d).truncate}
puts "Total\t#{total}\t#{seats}\t#{l_q_total}\t-\t#{app_total}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment