ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# conker22's solution

## to Grade School in the Elixir Track

Published at Sep 27 2020 · 0 comments
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
• "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?

## Running tests

Execute the tests with:

``````\$ mix test
``````

### Pending tests

In the test suites, all but the first test have been skipped.

Once you get a test passing, you can unskip the next one by commenting out the relevant `@tag :pending` with a `#` symbol.

For example:

``````# @tag :pending
test "shouting" do
assert Bob.hey("WATCH OUT!") == "Whoa, chill out!"
end
``````

Or, you can enable all the tests by commenting out the `ExUnit.configure` line in the test suite.

``````# ExUnit.configure exclude: :pending, trace: true
``````

If you're stuck on something, it may help to look at some of the available resources out there where answers might be found.

## 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.

### school_test.exs

``````defmodule SchoolTest do
use ExUnit.Case

@db %{}

test "add student" do
actual = School.add(@db, "Aimee", 2)
assert actual == %{2 => ["Aimee"]}
end

@tag :pending
test "add more students in same class" do
actual =
@db

assert Enum.sort(actual[2]) == ["Blair", "James", "Paul"]
end

@tag :pending
test "add students to different grades" do
actual =
@db

assert actual == %{3 => ["Chelsea"], 7 => ["Logan"]}
end

@tag :pending
test "get students in a grade" do
actual =
@db

assert Enum.sort(actual) == ["Bradley", "Franklin"]
end

@tag :pending
test "get students in a non existent grade" do
assert [] == School.grade(@db, 1)
end

@tag :pending
test "sort school by grade and by student name" do
actual =
@db
|> School.sort()

expected = [
{3, ["Kyle"]},
{4, ["Bart", "Christopher", "Jennifer"]},
{6, ["Kareem"]}
]

assert expected == actual
end
end``````

### test_helper.exs

``````ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)``````
``````defmodule School do
@moduledoc """
Simulate students in a school.

Each student is in a grade.
"""

@doc """
Add a student to a particular grade in school.
"""
@spec add(map, String.t(), integer) :: map
Map.update(db,grade, [name], fn x -> [name|x] end)
end

@doc """
Return the names of the students in a particular grade.
"""
@spec grade(map, integer) :: [String.t()]
:error -> []
{:ok,x} -> x
end

end

def sort(db), do: db |> Enum.map(fn {k, _v} -> {k, Enum.sort(grade(db, k))} end)

end``````