Avatar of 4d47
0
0
Genius
0
0

4d47's solution

to Grade School in the Perl 6 Track

0
0
Genius
0
0
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 prove6 . in the exercise directory.

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 JSON::Fast;
use lib $?FILE.IO.dirname;
use GradeSchool;
plan 7;

my $c-data = from-json $=pod.pop.contents;
for $c-data<cases>:v {
  cmp-ok roster(|%(<students grade> Z=> .<input><students desiredGrade>:v)), '~~', |.<expected description>;
}

=head2 Canonical Data
=begin code
{
    "exercise": "grade-school",
    "version": "1.0.0",
    "comments": [
        "Given students' names along with the grade that they are in, ",
        "create a roster for the school."
    ],
    "cases": [
        {
            "description": "Adding a student adds them to the sorted roster",
            "property": "roster",
            "input": {
              "students": [["Aimee", 2]]
            },
            "expected": ["Aimee"]
        },
        {
            "description": "Adding more student adds them to the sorted roster",
            "property": "roster",
            "input": {
              "students": [["Blair", 2], ["James", 2], ["Paul", 2]]
            },
            "expected": ["Blair", "James", "Paul"]
        },
        {
            "description": "Adding students to different grades adds them to the same sorted roster",
            "property": "roster",
            "input": {
              "students": [["Chelsea", 3], ["Logan", 7]]
            },
            "expected": ["Chelsea", "Logan"]
        },
        {
            "description": "Roster returns an empty list if there are no students enrolled",
            "property": "roster",
            "input": {
              "students": []
            },
            "expected": []
        },
        {
            "description": "Student names with grades are displayed in the same sorted roster",
            "property": "roster",
            "input": {
              "students": [["Peter", 2], ["Anna", 1], ["Barb", 1], ["Zoe", 2], ["Alex", 2], ["Jim", 3], ["Charlie", 1]]
            },
            "expected": ["Anna", "Barb", "Charlie", "Alex", "Peter", "Zoe", "Jim"]
        },
        {
            "description": "Grade returns the students in that grade in alphabetical order",
            "property": "grade",
            "input": {
              "students": [["Franklin", 5], ["Bradley", 5], ["Jeff", 1]],
              "desiredGrade": 5
            },
            "expected": ["Bradley", "Franklin"]
        },
        {
            "description": "Grade returns an empty list if there are no students in that grade",
            "property": "grade",
            "input": {
              "students": [],
              "desiredGrade": 1
            },
            "expected": []
        }
    ]
}
=end code
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?