DEV Community

Discussion on: Daily Challenge #68 - Grade Book

Collapse
 
choroba profile image
E. Choroba

Perl solution, using a regex to extract the last digit (but we need to replace 100 by 99 for it to work).

#!/usr/bin/perl
use warnings;
use strict;
use List::Util qw{ sum };

sub grade {
    my $mean = int(sum(@_) / @_);
    return 'F' if $mean < 60;

    $mean = 99 if $mean == 100;
    my $sign = ($mean =~ /(.)$/)[0] < 5 ? '-' : '+';

    return qw( D C B A )[ ($mean - 60) / 10 ] . $sign
}

use Test::More tests => 6;

is grade(100, 100, 100) => 'A+';
is grade( 60,  60,  60) => 'D-';
is grade( 64,  55,  92) => 'C-';
is grade( 99,  89,  93) => 'A-';
is grade( 33,  99,  95) => 'C+';
is grade( 60,  60,  59) => 'F';