Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- sum(floor(n/d) for n in ns) == N
- from math import floor
- l = [[190970, 156473], [138598, 173004], [143666, 193442], [1140370, 159468], [258275, 249049], [624, 819], [1125881], [152756], [118031], [74701]]
- def main(_l, bar, tot):
- # sum all votes to calculate bar in votes
- votes = sum(sum(_) for _ in _l)
- # nullify all parties that didn't pass the bar
- _l = [[__ if __ > bar * votes else 0 for __ in _] for _ in _l]
- # find divisor for all parliament seats
- divisor = find_divisor([sum(_) for _ in _l], 120)
- # find divisor for each 'coalition'
- divisors = [find_divisor(_, floor(sum(_)/divisor)) for _ in _l]
- # return final results
- return [[floor(___/_) for ___ in __] for _, __ in zip(divisors, _l)]
- def find_divisor(_l, tot, _min=0, _max=1):
- s = sum(floor(_ / _max) for _ in _l)
- if s == tot:
- return _max
- elif s < tot:
- return find_divisor(_l, tot, _min, (_max + _min) / 2)
- else:
- return find_divisor(_l, tot, _max, _max * 2)
- print(main(l, 0.0325, 120))
- [[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