Skip to content

Instantly share code, notes, and snippets.

@smls

smls/grades.p6 Secret

Last active August 29, 2015 14:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save smls/732115c66cd29aa2e9d0 to your computer and use it in GitHub Desktop.
Save smls/732115c66cd29aa2e9d0 to your computer and use it in GitHub Desktop.
#!/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