SHARE
TWEET

Untitled

a guest Jun 20th, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. arr = [7600, 5900, 1400]
  2. seats = 37
  3.  
  4. total = 0.0
  5.  
  6. arr.each { |e| total += e }
  7.  
  8. puts "Total: #{total}"
  9.  
  10. s_d = total / seats
  11. m_d = s_d
  12.  
  13. puts "SD: #{s_d}"
  14. puts
  15.  
  16. loop do
  17.   m_d -= 1 # Changed to 0.1 or lower if you get a divide by zero error
  18.  
  19.   sum = 0
  20.  
  21.   arr.each { |e| sum += (e / m_d).truncate}
  22.  
  23.   break if sum == seats
  24. end
  25.  
  26. puts "MD: #{m_d}"
  27. puts
  28.  
  29. puts "State\tPopulation\tQ\tLQ\tMQ\tApportionment"
  30.  
  31. arr.each do |v|
  32.   s_q1 = v / s_d
  33.   s_q2 = v / m_d
  34.  
  35.   puts "#{arr.index(v)}\t#{v}\t#{s_q1.truncate(3)}\t#{s_q1.truncate}\t#{s_q2.truncate(3)}\t#{s_q2.truncate}"
  36. end
  37.  
  38. l_q_total = 0
  39. arr.each { |e| l_q_total += (e / s_d).truncate}
  40.  
  41. app_total = 0
  42. arr.each { |e| app_total += (e / m_d).truncate}
  43.  
  44. puts "Total\t#{total}\t#{seats}\t#{l_q_total}\t-\t#{app_total}"
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top