my $votes_file = "votes.txt";
sub EffectiveVote ( Str $vote, Hash %skip ) {
return $vote.split( ',' ).first( { ! %skip{$_}.defined } )
// die "No valid vote?!";
}
sub CountRound (Array @votes, Hash %skip)
{
my %count;
for @votes -> $vote
{
%count{EffectiveVote($vote, %skip)}++;
}
return %count;
}
my @votes = do
{
my $votes = open($votes_file);
=$votes;
}
my $count = 0;
my %skip;
loop
{
say "\nRound {++$count}";
my @ranking = CountRound(@votes, %skip).pairs.sort({.value}).reverse;
say sprintf("%s: %s",.key, .value / @votes.elems)
for @ranking;
my $top_rank = @ranking[0];
if ($top_rank.value > @votes.elems / 2)
{
say "\nThe winner is {$top_rank.key} with {$top_rank.value / @votes.elems * 100.0}% of the vote.";
exit;
}
my $skip = @ranking.pop.key;
%skip{$skip} = 1;
say "Dropping $skip";
}
Third Script: Second Draft 0 Comments More | Login | Reply /