Scripting Games, part 3

bacek on 2008-12-24T07:31:03

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.