🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of angelikatyborska

angelikatyborska's solution

to Grade School in the Elixir Track

Published at Nov 06 2019 · 3 comments
Test suite

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?

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!"

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.


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.


defmodule SchoolTest do
  use ExUnit.Case

  @db %{}

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

  @tag :pending
  test "add more students in same class" do
    actual =
      |> School.add("James", 2)
      |> School.add("Blair", 2)
      |> School.add("Paul", 2)

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

  @tag :pending
  test "add students to different grades" do
    actual =
      |> School.add("Chelsea", 3)
      |> School.add("Logan", 7)

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

  @tag :pending
  test "get students in a grade" do
    actual =
      |> School.add("Bradley", 5)
      |> School.add("Franklin", 5)
      |> School.add("Jeff", 1)
      |> School.grade(5)

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

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

  @tag :pending
  test "sort school by grade and by student name" do
    actual =
      |> School.add("Bart", 4)
      |> School.add("Jennifer", 4)
      |> School.add("Christopher", 4)
      |> School.add("Kareem", 6)
      |> School.add("Kyle", 3)
      |> School.sort()

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

    assert expected == actual


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
  def add(db, name, grade) do
    Map.update(db, grade, [name], &[name | &1])

  @doc """
  Return the names of the students in a particular grade.
  @spec grade(map, integer) :: [String.t()]
  def grade(db, grade) do
    Map.get(db, grade, [])

  @doc """
  Sorts the school by grade and name.
  @spec sort(map) :: [{integer, [String.t()]}]
  def sort(db) do
    |> Enum.sort_by(&elem(&1, 0))
    |> Enum.map(fn {grade, students} -> {grade, Enum.sort(students)} end)

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of Gerrit

Hello mentor,

I really like the sort solution from


Enum.map(db, fn {k, v} -> {k, Enum.sort(v)} end)

Avatar of angelikatyborska

Hi Gerrit. That solution is not fully correct. Maps are not ordered data structures, and it's only a happy accident if they happen to behave like they are ordered in any given context. Thus the list of tuples with keys and values must be explicitly sorted by keys too.

Avatar of Gerrit

Thx, for the explanation.

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?
  • Are there new concepts here that you could read more about to improve your understanding?