So far not doing great, missed the deadline of the 2nd and 4th events and was too hasty in submitting the 3rd to get it right.
This quiz is a good feeler for what Perl other people write, and if you look at the difference from the official solution, Jan Dubois', and mine, there's a world of difference looking at Perl written by a non-Perl programmer. It's strange and alien looking to me, and explains why people harp on about Perl's ugliness.
Anyway here is my (fixed) solution to Advanced Event 3: Instant (Runoff) Winner:
#!usr/bin/perl -w
use strict;
use List::Util qw( sum first );
my %C; # candidates
open my $in, '<c:/scripts/votes.txt'
or die "Can't open 'votes.txt': $!\n";
while (1) {
while (<$in>) {
chomp;
my @vote = split/,/;
@C{@vote} = (0) x @vote if keys %C == 0;
$C{$_}++ for first { exists $C{$_} } @vote;
}
my @c = sort { $C{$b} <=> $C{$a} } keys %C;
my $percent = ($C{$c[0]} / (sum values %C)) * 100;
if ($percent > 50) {
printf "The winner is %s with %.2f%% of the vote.\n", $c[0], $percent;
last;
}
delete $C{pop @c};
@C{@c} = (0) x @c;
seek $in, 0, 0;
}
close $in;
Link to Jan Dubois's solution (Score:1)
2008 Winter Scripting Games: Advanced Perl Event 3 Solution
http://www.microsoft.com/technet/scriptcenter/funzone/games/solutions08/experls
Re: (Score:2)
“Strange and alien” (Score:1)
My suspicion is that someone with a Lisp background would write much more idiomatic Perl than someone with a Java/C# background… and I think I can tell what programmer wrote the official solution.