-
Notifications
You must be signed in to change notification settings - Fork 0
/
dHondt
executable file
·60 lines (52 loc) · 1.32 KB
/
dHondt
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
#!/usr/bin/perl -W
# Calculate seats in parliament according d'Hondt's method
# Elmar Klausmeier, 22-Feb-2014
use strict;
use Getopt::Std;
my %opts = ('d' => 0, 's' => 2);
getopts('ds:',\%opts);
my $s = (($opts{'s'} > 0) ? $opts{'s'} : 2);
my (@F,@votes,@party,@seat,@calc,@divisor,$i,$j,$n,$max,$multi,@drawing);
while (<>) {
@F = split;
push @party,$F[0];
push @votes,$F[1];
push @calc,$F[1];
}
print "Number of seats: $s\n";
$n = $#votes;
for ($i=0; $i<=$n; ++$i) {
print "\t$party[$i] - $votes[$i]\n";
}
# @votes does not need to be sorted in any way
for ($i=0; $i<=$n; ++$i) {
($seat[$i],$divisor[$i]) = (0,1);
}
# $u is used seats so far
for (my $u=0; $u<$s; ++$u) {
($max,$j) = ($calc[0],0); # calculate maximum in @calc
$drawing[$multi=0] = 0;
for ($i=1; $i<=$n; ++$i) {
if ($calc[$i] > $max) {
($max,$j) = ($calc[$i],$i);
$drawing[$multi=0] = $i;
} elsif ($calc[$i] == $max) {
$drawing[++$multi] = $i;
}
}
#print "multi=$multi\n";
if ($u + $multi >= $s) {
printf("Drawing of lots for %d seats required for parties:", $s - $u);
for ($i=0; $i<=$multi; ++$i) {
print " $party[$drawing[$i]]";
}
print "\n";
last;
}
$seat[$j] += 1;
$divisor[$j] += 1; # this is specific for d'Hondt
$calc[$j] = int($votes[$j] / $divisor[$j]);
}
for ($i=0; $i<=$n; ++$i) {
print "$party[$i] - $seat[$i]\n";
}