* deallocate seats properly

* simplify

* webster test

* missed conflict

* black
pull/10/head
Flynn 2019-10-13 22:13:32 -04:00 zatwierdzone przez GitHub
rodzic 8fbfed4585
commit 35e86a1dc1
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
2 zmienionych plików z 13 dodań i 3 usunięć

Wyświetl plik

@ -11,6 +11,10 @@ def test_apportionment_values(apportionment_method, votes, 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():
assert adams([10, 1], 2) == [1, 1]

Wyświetl plik

@ -183,13 +183,19 @@ def webster(votes, seats):
for k in range(unallocated):
assigned[divs[k]] += 1
elif unallocated < 0:
unallocated = abs(unallocated)
overallocated = abs(unallocated)
# divisors that would subtract a seat from each group
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)
]
# argsort
divs = [i[0] for i in sorted(enumerate(diffs), key=itemgetter(1))]
for k in range(unallocated):
assigned[divs[k]] -= 1
# deallocate seats without bringing seat allocation below zero
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