Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. sum(floor(n/d) for n in ns) == N
  2.  
  3. from math import floor
  4.  
  5. l = [[190970, 156473], [138598, 173004], [143666, 193442], [1140370, 159468], [258275, 249049], [624, 819], [1125881], [152756], [118031], [74701]]
  6.  
  7. def main(_l, bar, tot):
  8. # sum all votes to calculate bar in votes
  9. votes = sum(sum(_) for _ in _l)
  10.  
  11. # nullify all parties that didn't pass the bar
  12. _l = [[__ if __ > bar * votes else 0 for __ in _] for _ in _l]
  13.  
  14. # find divisor for all parliament seats
  15. divisor = find_divisor([sum(_) for _ in _l], 120)
  16.  
  17. # find divisor for each 'coalition'
  18. divisors = [find_divisor(_, floor(sum(_)/divisor)) for _ in _l]
  19.  
  20. # return final results
  21. return [[floor(___/_) for ___ in __] for _, __ in zip(divisors, _l)]
  22.  
  23.  
  24. def find_divisor(_l, tot, _min=0, _max=1):
  25. s = sum(floor(_ / _max) for _ in _l)
  26. if s == tot:
  27. return _max
  28. elif s < tot:
  29. return find_divisor(_l, tot, _min, (_max + _min) / 2)
  30. else:
  31. return find_divisor(_l, tot, _max, _max * 2)
  32.  
  33. print(main(l, 0.0325, 120))
  34.  
  35. [[6, 4], [0, 5], [4, 6], [35, 5], [8, 8], [0, 0], [35], [4], [0], [0]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement