grade-school

This commit is contained in:
Danil Negrienko 2024-07-02 22:36:32 -04:00
parent 2b1e10247b
commit 643a41d53c
10 changed files with 398 additions and 0 deletions

View File

@ -0,0 +1,41 @@
{
"authors": [
"rubysolo"
],
"contributors": [
"andrewsardone",
"angelikatyborska",
"bartj3",
"Cohen-Carlisle",
"dalexj",
"devonestes",
"drueck",
"elasticdog",
"jiegillet",
"jinyeow",
"kytrinyx",
"lpil",
"neenjaw",
"parkerl",
"petehuang",
"pminten",
"sotojuan",
"Teapane",
"veelenga",
"victorlcampos",
"waiting-for-dev"
],
"files": {
"solution": [
"lib/school.ex"
],
"test": [
"test/school_test.exs"
],
"example": [
".meta/example.ex"
]
},
"blurb": "Given students' names along with the grade that they are in, create a roster for the school.",
"source": "A pairing session with Phil Battos at gSchool"
}

View File

@ -0,0 +1 @@
{"track":"elixir","exercise":"grade-school","id":"9161318ecb0e46bc89207b97059c3866","url":"https://exercism.org/tracks/elixir/exercises/grade-school","handle":"negrienko","is_requester":true,"auto_approve":false}

View File

@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]

24
elixir/grade-school/.gitignore vendored Normal file
View File

@ -0,0 +1,24 @@
# The directory Mix will write compiled artifacts to.
/_build/
# If you run "mix test --cover", coverage assets end up here.
/cover/
# The directory Mix downloads your dependencies sources to.
/deps/
# Where third-party dependencies like ExDoc output generated docs.
/doc/
# Ignore .fetch files in case you like to edit your project deps locally.
/.fetch
# If the VM crashes, it generates a dump, let's ignore it too.
erl_crash.dump
# Also ignore archive artifacts (built via "mix archive.build").
*.ez
# Ignore package tarball (built via "mix hex.build").
grade_school-*.tar

View File

@ -0,0 +1,75 @@
# Help
## Running the tests
From the terminal, change to the base directory of the exercise then execute the tests with:
```bash
$ mix test
```
This will execute the test file found in the `test` subfolder -- a file ending in `_test.exs`
Documentation:
* [`mix test` - Elixir's test execution tool](https://hexdocs.pm/mix/Mix.Tasks.Test.html)
* [`ExUnit` - Elixir's unit test library](https://hexdocs.pm/ex_unit/ExUnit.html)
## Pending tests
In test suites of practice exercises, all but the first test have been tagged to be 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:
```elixir
# @tag :pending
test "shouting" do
assert Bob.hey("WATCH OUT!") == "Whoa, chill out!"
end
```
If you wish to run all tests at once, you can include all skipped test by using the `--include` flag on the `mix test` command:
```bash
$ mix test --include pending
```
Or, you can enable all the tests by commenting out the `ExUnit.configure` line in the file `test/test_helper.exs`.
```elixir
# ExUnit.configure(exclude: :pending, trace: true)
```
## Useful `mix test` options
* `test/<FILE>.exs:LINENUM` - runs only a single test, the test from `<FILE>.exs` whose definition is on line `LINENUM`
* `--failed` - runs only tests that failed the last time they ran
* `--max-failures` - the suite stops evaluating tests when this number of test failures
is reached
* `--seed 0` - disables randomization so the tests in a single file will always be ran
in the same order they were defined in
## Submitting your solution
You can submit your solution using the `exercism submit lib/school.ex` command.
This command will upload your solution to the Exercism website and print the solution page's URL.
It's possible to submit an incomplete solution which allows you to:
- See how others have completed the exercise
- Request help from a mentor
## Need to get help?
If you'd like help solving the exercise, check the following pages:
- The [Elixir track's documentation](https://exercism.org/docs/tracks/elixir)
- The [Elixir track's programming category on the forum](https://forum.exercism.org/c/programming/elixir)
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
If you're stuck on something, it may help to look at some of the [available resources](https://exercism.org/docs/tracks/elixir/resources) out there where answers might be found.

View File

@ -0,0 +1,60 @@
# Grade School
Welcome to Grade School on Exercism's Elixir Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## Instructions
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?"
- "Let me think.
We have Anna, Barb, and Charlie in grade 1, Alex, Peter, and Zoe in grade 2 and Jim in grade 5.
So the answer is: Anna, Barb, Charlie, Alex, Peter, Zoe and Jim"
Note that all our students only have one name (It's a small town, what do you want?) and each student cannot be added more than once to a grade or the roster.
In fact, when a test attempts to add the same student more than once, your implementation should indicate that this is incorrect.
## Source
### Created by
- @rubysolo
### Contributed to by
- @andrewsardone
- @angelikatyborska
- @bartj3
- @Cohen-Carlisle
- @dalexj
- @devonestes
- @drueck
- @elasticdog
- @jiegillet
- @jinyeow
- @kytrinyx
- @lpil
- @neenjaw
- @parkerl
- @petehuang
- @pminten
- @sotojuan
- @Teapane
- @veelenga
- @victorlcampos
- @waiting-for-dev
### Based on
A pairing session with Phil Battos at gSchool

View File

@ -0,0 +1,44 @@
defmodule School do
@moduledoc """
Simulate students in a school.
Each student is in a grade.
"""
@type school :: any()
@doc """
Create a new, empty school.
"""
@spec new() :: school
def new(), do: %{}
@doc """
Add a student to a particular grade in school.
"""
@spec add(school, String.t(), integer) :: {:ok | :error, school}
def add(school, name, grade) do
if Enum.any?(roster(school), &(&1 == name)) do
{:error, school}
else
{:ok, Map.put(school, grade, [name | Map.get(school, grade, [])])}
end
end
@doc """
Return the names of the students in a particular grade, sorted alphabetically.
"""
@spec grade(school, integer) :: [String.t()]
def grade(school, grade) when not is_map_key(school, grade), do: []
def grade(school, grade), do: Enum.sort(Map.get(school, grade))
@doc """
Return the names of all the students in the school sorted by grade and name.
"""
@spec roster(school) :: [String.t()]
def roster(school) do
for grade <- Enum.sort(Map.keys(school)), reduce: [] do
acc -> acc ++ grade(school, grade)
end
end
end

View File

@ -0,0 +1,28 @@
defmodule School.MixProject do
use Mix.Project
def project do
[
app: :school,
version: "0.1.0",
# elixir: "~> 1.8",
start_permanent: Mix.env() == :prod,
deps: deps()
]
end
# Run "mix help compile.app" to learn about applications.
def application do
[
extra_applications: [:logger]
]
end
# Run "mix help deps" to learn about dependencies.
defp deps do
[
# {:dep_from_hexpm, "~> 0.3.0"},
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
]
end
end

View File

@ -0,0 +1,119 @@
defmodule SchoolTest do
use ExUnit.Case
def make_school_with_students(students) do
{results, school} =
Enum.reduce(students, {[], School.new()}, fn {student, grade}, {results, school} ->
{result, school} = School.add(school, student, grade)
{[result | results], school}
end)
{Enum.reverse(results), school}
end
test "Roster is empty when no student is added" do
assert School.roster(School.new()) == []
end
test "Add a student" do
{result, school} = School.add(School.new(), "Aimee", 2)
assert result == :ok
assert School.roster(school) == ["Aimee"]
end
test "Adding multiple students in the same grade in the roster" do
students = [{"Blair", 2}, {"James", 2}, {"Paul", 2}]
{results, school} = make_school_with_students(students)
assert results == [:ok, :ok, :ok]
assert School.roster(school) == ["Blair", "James", "Paul"]
end
test "Cannot add student to same grade in the roster more than once" do
students = [{"Blair", 2}, {"James", 2}, {"James", 2}, {"Paul", 2}]
{results, school} = make_school_with_students(students)
assert results == [:ok, :ok, :error, :ok]
assert School.roster(school) == ["Blair", "James", "Paul"]
end
test "Adding students in multiple grades" do
students = [{"Chelsea", 3}, {"Logan", 7}]
{results, school} = make_school_with_students(students)
assert results == [:ok, :ok]
assert School.roster(school) == ["Chelsea", "Logan"]
end
test "Cannot add same student to multiple grades in the roster" do
students = [{"Blair", 2}, {"James", 2}, {"James", 3}, {"Paul", 3}]
{results, school} = make_school_with_students(students)
assert results == [:ok, :ok, :error, :ok]
assert School.roster(school) == ["Blair", "James", "Paul"]
end
test "Students are sorted by grades in the roster" do
students = [{"Jim", 3}, {"Peter", 2}, {"Anna", 1}]
{_results, school} = make_school_with_students(students)
assert School.roster(school) == ["Anna", "Peter", "Jim"]
end
test "Students are sorted by name in the roster" do
students = [{"Peter", 2}, {"Zoe", 2}, {"Alex", 2}]
{_results, school} = make_school_with_students(students)
assert School.roster(school) == ["Alex", "Peter", "Zoe"]
end
test "Students are sorted by grades and then by name in the roster" do
students = [
{"Peter", 2},
{"Anna", 1},
{"Barb", 1},
{"Zoe", 2},
{"Alex", 2},
{"Jim", 3},
{"Charlie", 1}
]
{_results, school} = make_school_with_students(students)
assert School.roster(school) == ["Anna", "Barb", "Charlie", "Alex", "Peter", "Zoe", "Jim"]
end
test "Grade is empty if no students in the roster" do
assert School.grade(School.new(), 1) == []
end
test "Grade is empty if no students in that grade" do
students = [{"Peter", 2}, {"Zoe", 2}, {"Alex", 2}, {"Jim", 3}]
{_results, school} = make_school_with_students(students)
assert School.grade(school, 1) == []
end
test "Student not added to same grade more than once" do
students = [{"Blair", 2}, {"James", 2}, {"James", 2}, {"Paul", 2}]
{_results, school} = make_school_with_students(students)
assert School.roster(school) == ["Blair", "James", "Paul"]
end
test "Student not added to multiple grades" do
students = [{"Blair", 2}, {"James", 2}, {"James", 3}, {"Paul", 3}]
{_results, school} = make_school_with_students(students)
assert School.grade(school, 2) == ["Blair", "James"]
assert School.grade(school, 3) == ["Paul"]
end
test "Students are sorted by name in a grade" do
students = [{"Franklin", 5}, {"Bradley", 5}, {"Jeff", 1}]
{_results, school} = make_school_with_students(students)
assert School.grade(school, 5) == ["Bradley", "Franklin"]
end
end

View File

@ -0,0 +1,2 @@
ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)