Instantly share code, notes, and snippets.

@smls /grades.p6 Secret
Last active Aug 29, 2015

Embed
What would you like to do?
#!/usr/bin/env perl6
my %grade = "grades.txt".IO.lines.map: {
m:s/^ (\w+) (<[A..F]><[+-]>?) $/ or die "Can't parse line '$_'";
~$0 => ~$1
};
say "Zsófia's grade: %grade<Zsófia>";
say "List of students with a failing grade:";
say " " ~ %grade.grep(*.value ge "E".key.join(', ');
say "Distribution of grades by letter:";
say " {.key}: {+.value} student{"s" if .value != 1}"
for %grade.classify(*.value.comb[0]).sort(*.key);
#!/usr/bin/env perl
use warnings;
use strict;
use feature 'say';
use utf8;
binmode STDOUT, ":utf8";
open my $fh, '<:utf8', "grades.txt" or die "Failed to open file: $!";
my %grade;
while (<$fh>) {
m/^ (\w+) \s+ ([A-F][+-]?) $/x or die "Can't parse line '$_'";
$grade{$1} = $2;
};
say "Zsófia's grade: $grade{Zsófia}";
say "List of students with a failing grade:";
say " " . join ", ", grep { $grade{$_} ge "E" } keys %grade;
say "Distribution of grades by letter:";
my %freq;
$freq{substr $grade{$_}, 0, 1}++ for keys %grade;
say " $_: $freq{$_} student".($freq{$_} != 1 ? "s" : "")
for sort keys %freq;
Peter B
Celine A-
Zsófia B+
João F
Maryam B+
秀英 B-
Finn D+
Aarav A
Emma F
Omar B
#!/usr/bin/env perl6
my %grade = "grades.txt".IO.lines.map: {
m:s/^ (\w+) (<[A..F]><[+-]>?) $/ or die "Can't parse line '$_'";
~$0 => ~$1
};
say "Zsófia's grade: %grade<Zsófia>";
say "List of students with a failing grade:";
say " " ~ %grade.grep(*.value ge "E".key.join(', ');
say "Distribution of grades by letter:";
say " $(.key): $(+.value) student$("s" if .value != 1)"
for %grade.classify(*.value.comb[0]).sort(*.key);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment