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 ]

bacek (8813)

bacek
  (email not shown publicly)

Journal of bacek (8813)

Wednesday December 24, 2008
02:31 AM

Scripting Games, part 3

[ #38148 ]

Next one: find winner of skater contest.

        # Calculate score for single result
        sub calculate_score(@scores) {
                # Sort scores
                my @sorted = sort @scores;
                # Drop highest and lowest
                shift @sorted;
                pop @sorted;
                # Calculate final score
                ([+] @sorted)/ +@sorted;
        };

        # Parse line in form "Ken Myer,55,66,76,67,59,70,54", and return (score, name)
        sub parse_line($line) {
                my @parts = $line.split(',');
                reverse(shift @parts, calculate_score(@parts));
        };

        # Main cycle. Read file, parse lines, build mapping score=>name
        my $fh = open('skaters.txt') or die;
        my %results = map &parse_line, =$fh;

        # Get 3 top results
        my @top = %results.keys.sort.reverse[0..2];
        # And show them
        say %results{$_}, ': ', $_ for @top;

I don't know what to say. Solution is straight forward.

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.