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:
Note that all our students only have one name. (It's a small town, what do you want?)
Did you get the tests passing and the code clean? If you want to, these are some additional things you could try:
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?
Execute the tests with:
$ elixir grade_school_test.exs
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.
A pairing session with Phil Battos at gSchool http://gschool.it
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
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(Dict.t, String.t, pos_integer) :: Dict.t
# changed param name not to conflict w/ function
def add(db, name, grd) do
Map.put(db, grd, [name | grade(db, grd)])
end
@doc """
Return the names of the students in a particular grade.
"""
@spec grade(Dict.t, pos_integer) :: [String]
def grade(db, grade) do
Map.get(db, grade, [])
end
@doc """
Sorts the school by grade and name.
"""
@spec sort(Dict) :: Dict.t
def sort(db) do
db |> Enum.sort # returns it in key-sorted order, but AS TUPLES!
|> Enum.map(&(sort_students(&1)))
|> Enum.into(%{})
end
# takes grade as *tuple*, since that's what Enum.sort returns a list of :-(
defp sort_students(grade), do: put_elem(grade, 1, Enum.sort(elem(grade, 1)))
end
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.
Level up your programming skills with 3,126 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments