-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelections.php
More file actions
64 lines (58 loc) · 1.78 KB
/
Copy pathelections.php
File metadata and controls
64 lines (58 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
class HareNiemeier
{
protected $_parties;
protected $_votes;
public function setParties(array $parties)
{
if(!count($parties)) {
throw new Exception('Number of parties is not valid.');
}
$this->_parties = $parties;
}
public function setVotesToParties($votes)
{
if(!count($this->_parties)) {
throw new Exception('Set parties first.');
}
if(count($this->_parties) != count($votes)) {
throw new Exception('Count votes != count parties.');
}
$this->_votes = $votes;
}
public function calculate($seats)
{
if(!is_int($seats)) {
throw new Exception('Number of seats is not valid.');
}
$sumVotes = array_sum($this->_votes);
$calc = function (&$val) use ($sumVotes, $seats){
return ($val * $seats) / $sumVotes;
};
$firstStep= array_map($calc, $this->_votes);
$getCeil = function ($partyFirstStep) {
return floor($partyFirstStep);
};
$getFractional = function ($partyFirstStep) {
return $partyFirstStep - floor($partyFirstStep);
};
$ceils = array_map($getCeil, $firstStep);
$fractional = array_map($getFractional, $firstStep);
$leftSeats = $seats - array_sum($ceils);
if ($leftSeats) {
arsort($fractional);
foreach ($fractional as $key => $val) {
$ceils[$key]++;
$leftSeats--;
if ($leftSeats == 0) {
break;
}
}
}
return $ceils;
}
}
$t = new HareNiemeier();
$t->setParties(array('a', 'b', 'c', 'd'));
$t->setVotesToParties(array(15000, 5400, 5500, 5550));
var_dump($t->calculate(15));