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

# mathewdbutton's solution

## to Grade School in the Elixir Track

Published at Oct 18 2020 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution was written.

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:

• "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?"

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:

``````\$ elixir grade_school_test.exs
``````

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

``````if !System.get_env("EXERCISM_TEST_EXAMPLES") do
end

ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)

defmodule SchoolTest do
use ExUnit.Case

@db %{}

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
actual =
@db

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

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

end

@tag :pending
test "get students in a non existent grade" do
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``````
``````defmodule School do
@moduledoc """
Simulate students in a school.

Each student is in a grade.
"""

@doc """
"""
@spec add(map, String.t(), integer) :: map
Map.update(db, grade, name, fn cv -> [name | cv] end)
else
end
end

@doc """
Return the names of the students in a particular grade.
"""
end

@doc """
Sorts the school by grade and name.
"""
@spec sort(map) :: [{integer, [String.t()]}]
def sort(db) do
Enum.map(db, fn {k, v} -> {k, Enum.sort(v)} end)
end
end``````