Winter Scripting Games II

gav on 2008-02-23T01:11:07

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, ') {
        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

mr_bean on 2008-02-23T02:37:55

It is:
2008 Winter Scripting Games: Advanced Perl Event 3 Solution
http://www.microsoft.com/technet/scriptcenter/funzone/games/solutions08/experlso l03.mspx
 

Re:Link to Jan Dubois's solution

gav on 2008-02-23T02:47:21

Thats, my copy & paste failure fixed.

&#8220;Strange and alien&#8221;

Aristotle on 2008-02-23T10:44:41

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.