-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added 'democracy_representation' and unittests
- Loading branch information
1 parent
8066424
commit fa082b1
Showing
2 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from operator import truediv | ||
|
||
|
||
def representation(zone_pop, rep_req): | ||
rep_total = 0 | ||
result = [] | ||
population_total = sum(zone_pop) | ||
for population in zone_pop: | ||
# rep = (population / population_total) * rep_req # Python 3 | ||
rep = truediv(population, population_total) * rep_req | ||
# current = round(rep) or 1 # Python 3 | ||
current = round(rep) if rep % 1 > 0.5 else int(rep) or 1 | ||
rep_total += current | ||
result.append(current) | ||
diff = rep_total - rep_req | ||
# for _ in range(abs(int(diff))): # Python 3 | ||
for _ in xrange(abs(int(diff))): | ||
if diff < 0: | ||
result[result.index(min(result))] += 1 | ||
diff += 1 | ||
else: | ||
result[result.index(max(result))] -= 1 | ||
diff -= 1 | ||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import unittest | ||
|
||
from katas.beta.democracy_representation import representation | ||
|
||
|
||
class RepresentationTestCase(unittest.TestCase): | ||
def test_equal_1(self): | ||
self.assertEqual(representation([50, 25, 25, 100], 20), [5, 3, 2, 10]) | ||
|
||
def test_equal_2(self): | ||
self.assertEqual(representation([50, 25, 25, 100], 4), [1, 1, 1, 1]) | ||
|
||
def test_equal_3(self): | ||
self.assertEqual(representation([50, 25, 25, 100], 7), [2, 1, 1, 3]) | ||
|
||
def test_equal_4(self): | ||
self.assertEqual(representation( | ||
[4802982, 721523, 6412700, 2926229, 37341989, 5044930, 3581628, | ||
900877, 18900773, 9727566, 1366862, 1573499, 12864380, 6501582, | ||
3053787, 2863813, 4350606, 4553962, 1333074, 5789929, 6559644, | ||
9911626, 5314879, 2978240, 6011478, 994416, 1831825, 2709432, | ||
1321445, 8807501, 2067273, 19421055, 9565781, 675905, 11568495, | ||
3764882, 3848606, 12734905, 1055247, 4645975, 819761, 6375431, | ||
25268418, 2770765, 630337, 8037736, 6753369, 1859815, 5698230, | ||
568300], 435), | ||
[7, 2, 9, 4, 53, 7, 5, 2, 27, 14, 2, 2, 18, 9, 4, 4, 6, 6, 2, 8, | ||
9, 14, 7, 4, 8, 1, 3, 4, 2, 12, 3, 27, 13, 1, 16, 5, 5, 18, 1, | ||
7, 1, 9, 36, 4, 1, 11, 10, 3, 8, 1]) | ||
|
||
def test_equal_5(self): | ||
self.assertEqual(representation( | ||
[17448256, 5526120, 39104121, 14943910, 25138150, 6199424, | ||
36820195, 47242577, 5783258, 15070647, 4742650, 27678364, | ||
6084338, 29409647, 24014328, 42030125, 26879965, 2542568, | ||
23833049], 198), | ||
[9, 3, 19, 7, 12, 3, 18, 23, 3, 7, 2, 14, 3, 15, 12, 21, 13, 2, 12] | ||
) | ||
|
||
def test_equal_6(self): | ||
self.assertEqual(representation( | ||
[30046741, 22181701, 46356619, 34701454, 10859786, 14042362, | ||
10900456, 32908857, 7324040, 21367958, 34297544, 49901256, | ||
17399356, 2013729, 40315508, 4358397, 46411338, 3209697, | ||
5094747, 37367506, 47824945, 32540644, 13248635, 3106602, | ||
35991902, 24401136, 35721123, 29897187], 180), | ||
[8, 6, 12, 9, 3, 4, 3, 9, 2, 6, 9, 12, 5, 1, 10, 1, 12, 1, 1, 10, | ||
12, 8, 3, 1, 9, 6, 9, 8]) | ||
|
||
def test_equal_7(self): | ||
self.assertEqual(representation( | ||
[2762402, 44775797, 26474, 38475924, 46009952, 2546758, 40950651, | ||
8757078, 41568806, 40373888, 22340415, 38333457, 25746820, | ||
40960509, 19835731, 23787937, 31609639, 16535550, 48795217, | ||
20411929, 46049919, 18615557, 15307755, 34050119, 27207999, | ||
40210444, 42096485, 46134568, 32918012, 19580094, 19923438, | ||
31288306, 41228127], 255), | ||
[1, 12, 1, 10, 12, 1, 11, 2, 11, 11, 6, 10, 7, 11, 5, 6, 8, 4, 12, | ||
5, 12, 5, 4, 9, 7, 11, 11, 12, 9, 5, 5, 8, 11]) | ||
|
||
def test_equal_8(self): | ||
self.assertEqual(representation( | ||
[37325745, 5727614, 44158263, 45333329, 38680026, 48204256, | ||
31221948, 13853232, 36029436, 39755341, 44489627, 692631, | ||
4757880, 14677409, 10320411, 35260923, 1294543, 24247695, | ||
30535258, 23749996, 19227611, 40377666, 43423795, 24888067, | ||
14869523, 41782537, 18896357, 1536374, 5771178, 16245870, | ||
19813978, 11960415, 12741934, 21417662, 44188080, 21947958, | ||
44836098, 23815205, 27356901, 2483399, 28519663, 43810595, | ||
37614694, 1806175, 1881072, 25729643, 40738638, 7775397], 314), | ||
[10, 2, 11, 11, 10, 11, 8, 4, 10, 11, 12, 1, 1, 4, 3, 9, 1, 6, 8, | ||
6, 5, 11, 12, 7, 4, 11, 5, 1, 2, 4, 5, 3, 3, 6, 12, 6, 12, 6, 7, | ||
1, 8, 12, 10, 1, 1, 7, 11, 2]) | ||
|
||
def test_equal_9(self): | ||
self.assertEqual(representation( | ||
[14324593, 33584647, 4087427, 5149761, 463162, 17717186, 22720463, | ||
7176124, 7869907, 16964606, 47947574, 32185295, 16093401, 1311174, | ||
15220263, 5502205, 7880577, 2677607, 36892894, 19338178, 46860313, | ||
43220765, 49430796, 40287140, 35452847, 810, 8164673], 270), | ||
[7, 17, 2, 3, 1, 9, 11, 4, 4, 9, 23, 16, 8, 1, 8, 3, 4, 1, 18, 10, | ||
23, 22, 23, 20, 18, 1, 4]) |