Avatar of paulfioravanti

paulfioravanti's solution

to Grade School in the Elixir Track

Published at Jul 01 2019 · 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:

  • 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:

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

grade_school_test.exs

if !System.get_env("EXERCISM_TEST_EXAMPLES") do
  Code.load_file("school.exs", __DIR__)
end

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

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
      |> School.add("James", 2)
      |> School.add("Blair", 2)
      |> School.add("Paul", 2)

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

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

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

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

    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.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
  end
end
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
    db
    |> Map.update(grade, [name], fn names -> [name | names] end)
  end

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

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

  defp to_enrollment({grade, students}) do
    {grade, Enum.sort(students)}
  end
end

Community comments

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

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?