Avatar of 4d47
0
0
Genius
0
0

4d47's solution

to Grade School in the Perl 6 Track

Instructions
Test suite
Solution

Given students' names along with the grade that they are in, create a roster for the school.

In the end, you should be able to:

  • Add a student's name to the roster for a grade
    • "Add Jim to grade 2."
    • "OK."
  • Get a list of all students enrolled in a grade
    • "Which students are in grade 2?"
    • "We've only got Jim just now."
  • Get a sorted list of all students in all grades. Grades should sort as 1, 2, 3, etc., and students within a grade should be sorted alphabetically by name.
    • "Who all is enrolled in school right now?"
    • "Grade 1: Anna, Barb, and Charlie. Grade 2: Alex, Peter, and Zoe. Grade 3…"

Note that all our students only have one name. (It's a small town, what do you want?)

For bonus points

Did you get the tests passing and the code clean? If you want to, these are some additional things you could try:

  • If you're working in a language with mutable data structures and your implementation allows outside code to mutate the school's internal DB directly, see if you can prevent this. Feel free to introduce additional tests.

Then please share your thoughts in a comment on the submission. Did this experiment make the code better? Worse? Did you learn anything from it?

Resources

Remember to check out the Perl 6 documentation and resources pages for information, tips, and examples if you get stuck.

Running the tests

There is a test suite and module included with the exercise. The test suite (a file with the extension .t) will attempt to run routines from the module (a file with the extension .pm6). Add/modify routines in the module so that the tests will pass! You can view the test data by executing the command perl6 --doc *.t (* being the name of the test suite), and run the test suite for the exercise by executing the command prove . --exec=perl6 in the exercise directory. You can also add the -v flag e.g. prove . --exec=perl6 -v to display all tests, including any optional tests marked as 'TODO'.

Source

A pairing session with Phil Battos at gSchool http://gschool.it

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

grade-school.t

#!/usr/bin/env perl6
use v6;
use Test;
use lib $?FILE.IO.dirname;
use GradeSchool;
plan 18;

my Version:D $version = v3;

if GradeSchool.^ver !~~ $version {
  warn "\nExercise version mismatch. Further tests may fail!"
    ~ "\nGradeSchool is {GradeSchool.^ver.gist}. "
    ~ "Test is {$version.gist}.\n";
}

subtest "Roster class methods", {
  plan 3;
  ok ::('Roster').can($_), $_ for <add-student list-grade list-all>;
}

my $roster = ::('Roster').new;

ok $roster.?add-student(:name('Jim'), :2grade), 'Add Jim to grade 2';
cmp-ok $roster.?list-grade(2), '~~', <Jim>, 'List grade 2';

ok $roster.?add-student(:name('Zoe'), :2grade), 'Add Zoe to grade 2';
ok $roster.?add-student(:name('Barb'), :1grade), 'Add Barb to grade 1';
cmp-ok $roster.?list-grade(2), '~~', <Jim Zoe>, 'List grade 2';
cmp-ok $roster.?list-grade(1), '~~', <Barb>, 'List grade 1';

cmp-ok $roster.?list-all, '~~', ('Grade 1', <Barb>, 'Grade 2', <Jim Zoe>), 'List all';

ok $roster.?add-student(:name($_), :1grade), "Add $_ to grade 1" for <Charlie Anna>;
ok $roster.?add-student(:name('Alex'), :2grade), 'Add Alex to grade 2';
ok $roster.?add-student(:name($_), :3grade), "Add $_ to grade 3" for <Tom Dick Harry>;

cmp-ok $roster.?list-grade(1), '~~', <Anna Barb Charlie>, 'List grade 1';
cmp-ok $roster.?list-grade(2), '~~', <Alex Jim Zoe>, 'List grade 2';
cmp-ok $roster.?list-grade(3), '~~', <Dick Harry Tom>, 'List grade 3';
cmp-ok $roster.?list-all, '~~', ('Grade 1', <Anna Barb Charlie>, 'Grade 2', <Alex Jim Zoe>, 'Grade 3', <Dick Harry Tom>), 'List all';
unit class Roster is export;

subset PositiveInt of Int where * >= 1;

has @!students;

method add-student(Str :$name, PositiveInt :$grade) {
    @!students[$grade].push($name)
}
method list-grade(PositiveInt $grade) {
    @!students[$grade]
}
method list-all {
    @!students.kv.map: -> $grade, $names {
        if $names {
            ("Grade $grade", $names.sort)
        }
    }
}

What can you learn from this solution?

A huge amount can be learnt from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that I could read more about to develop my understanding?