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:
$ mix test
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.
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.
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
ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)
defmodule School do
@spec add(map, String.t(), integer) :: map
def add(db, name, grade) do
Map.update(db, grade, [name], fn students -> [name | students] end)
end
@spec grade(map, integer) :: [String.t()]
def grade(db, grade) do
db[grade] || []
end
@spec sort(map) :: [{integer, [String.t()]}]
def sort(db) do
Enum.map(db, fn {k, v} -> {k, Enum.sort(v)} end)
end
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,450 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