Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

gav (2710)

gav
  (email not shown publicly)
http://www.estey.com/
AOL IM: flufflegavin (Add Buddy, Send Message)

Hacker in NYC.

Journal of gav (2710)

Friday February 22, 2008
08:11 PM

Winter Scripting Games II

[ #35729 ]

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;

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.