kopia lustrzana https://github.com/crflynn/voting
Webster fix (#9)
* deallocate seats properly * simplify * webster test * missed conflict * blackpull/10/head
rodzic
8fbfed4585
commit
35e86a1dc1
|
@ -11,6 +11,10 @@ def test_apportionment_values(apportionment_method, votes, seats_val):
|
||||||
assert sum(apportionment_method(votes, seats_val)) == seats_val
|
assert sum(apportionment_method(votes, seats_val)) == seats_val
|
||||||
|
|
||||||
|
|
||||||
|
def test_webster():
|
||||||
|
assert all([a >= 0 for a in webster([2057, 2496, 2059, 181], 15)])
|
||||||
|
|
||||||
|
|
||||||
def test_adams():
|
def test_adams():
|
||||||
assert adams([10, 1], 2) == [1, 1]
|
assert adams([10, 1], 2) == [1, 1]
|
||||||
|
|
||||||
|
|
|
@ -183,13 +183,19 @@ def webster(votes, seats):
|
||||||
for k in range(unallocated):
|
for k in range(unallocated):
|
||||||
assigned[divs[k]] += 1
|
assigned[divs[k]] += 1
|
||||||
elif unallocated < 0:
|
elif unallocated < 0:
|
||||||
unallocated = abs(unallocated)
|
overallocated = abs(unallocated)
|
||||||
# divisors that would subtract a seat from each group
|
# divisors that would subtract a seat from each group
|
||||||
diffs = [
|
diffs = [
|
||||||
1.0 * vote / (i + 0.5) if dec >= 0.5 else 1.0 * vote / (i - 0.5) for i, dec, vote in zip(lower, decs, votes)
|
1.0 * vote / (i + 0.5) if dec >= 0.5 else 1.0 * vote / (i - 0.5) for i, dec, vote in zip(lower, decs, votes)
|
||||||
]
|
]
|
||||||
# argsort
|
# argsort
|
||||||
divs = [i[0] for i in sorted(enumerate(diffs), key=itemgetter(1))]
|
divs = [i[0] for i in sorted(enumerate(diffs), key=itemgetter(1))]
|
||||||
for k in range(unallocated):
|
# deallocate seats without bringing seat allocation below zero
|
||||||
assigned[divs[k]] -= 1
|
div_idx = 0
|
||||||
|
while overallocated > 0:
|
||||||
|
if assigned[divs[div_idx]] > 0:
|
||||||
|
assigned[divs[div_idx]] -= 1
|
||||||
|
overallocated -= 1
|
||||||
|
else:
|
||||||
|
div_idx += 1
|
||||||
return assigned
|
return assigned
|
||||||
|
|
Ładowanie…
Reference in New Issue