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

# shmibs's solution

## to Grade School in the Elixir Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

#### Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code 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
``````

For more detailed information about the Elixir track, please see the help page.

## 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
cond do
true -> Map.put db, grade, [name]
end
end

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

@doc """
Sorts the school by grade and name.
"""
@spec sort(map) :: [{integer, [String.t]}]
def sort(db) do
Map.keys(db)
|> Enum.sort
|> Enum.map( &({&1, grade(db, &1)}) )
end
end``````

### What can you learn from this solution?

A huge amount can be learned 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?