Skip to content

Commit

Permalink
Finish grade school problem
Browse files Browse the repository at this point in the history
  • Loading branch information
sirius248 committed Sep 17, 2015
1 parent 3203d34 commit 26e0295
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 0 deletions.
47 changes: 47 additions & 0 deletions grade-school/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Grade School

Write a small archiving program that stores students' names along with the grade that they are in.

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

```bash
$ elixir bob_test.exs
```

(Replace `bob_test.exs` with the name of the test file.)

## Source

A pairing session with Phil Battos at gSchool [view source](http://gschool.it)
69 changes: 69 additions & 0 deletions grade-school/grade_school_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
if System.get_env("EXERCISM_TEST_EXAMPLES") do
Code.load_file("example.exs")
else
Code.load_file("school.exs")
end

ExUnit.start
ExUnit.configure(exclude: :pending)

defmodule SchoolTest do
use ExUnit.Case, async: true

def db, do: %{}

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

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

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

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

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

test "get students in a non existant grade" do
assert [] == School.grade(db, 1)
end

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
42 changes: 42 additions & 0 deletions grade-school/school.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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
def add(db, name, grade) do
if Dict.has_key?(db, grade) do
Dict.put(db, grade, Dict.get(db, grade) ++ [name])
else
Dict.put(db, grade, [name])
end
end

@doc """
Return the names of the students in a particular grade.
"""
@spec grade(Dict.t, pos_integer) :: [String]
def grade(db, grade) do
if Dict.has_key?(db, grade), do: Dict.get(db, grade), else: []
end

@doc """
Sorts the school by grade and name.
"""
@spec sort(Dict) :: Dict.t
def sort(db) do
sort_help(db, Dict.keys(db), %{})
end

def sort_help(db, keys, r) do
case keys do
[] -> r
[head|tail] -> sort_help(db, tail, Dict.put(r, head, Enum.sort(grade(db, head))))
end
end
end

0 comments on commit 26e0295

Please sign in to comment.