From 643a41d53c0161fc14eb19e9e75dab14bae28875 Mon Sep 17 00:00:00 2001 From: Danylo Negrienko Date: Tue, 2 Jul 2024 22:36:32 -0400 Subject: [PATCH] grade-school --- elixir/grade-school/.exercism/config.json | 41 +++++++ elixir/grade-school/.exercism/metadata.json | 1 + elixir/grade-school/.formatter.exs | 4 + elixir/grade-school/.gitignore | 24 ++++ elixir/grade-school/HELP.md | 75 ++++++++++++ elixir/grade-school/README.md | 60 ++++++++++ elixir/grade-school/lib/school.ex | 44 ++++++++ elixir/grade-school/mix.exs | 28 +++++ elixir/grade-school/test/school_test.exs | 119 ++++++++++++++++++++ elixir/grade-school/test/test_helper.exs | 2 + 10 files changed, 398 insertions(+) create mode 100644 elixir/grade-school/.exercism/config.json create mode 100644 elixir/grade-school/.exercism/metadata.json create mode 100644 elixir/grade-school/.formatter.exs create mode 100644 elixir/grade-school/.gitignore create mode 100644 elixir/grade-school/HELP.md create mode 100644 elixir/grade-school/README.md create mode 100644 elixir/grade-school/lib/school.ex create mode 100644 elixir/grade-school/mix.exs create mode 100644 elixir/grade-school/test/school_test.exs create mode 100644 elixir/grade-school/test/test_helper.exs diff --git a/elixir/grade-school/.exercism/config.json b/elixir/grade-school/.exercism/config.json new file mode 100644 index 0000000..e57acd3 --- /dev/null +++ b/elixir/grade-school/.exercism/config.json @@ -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" +} diff --git a/elixir/grade-school/.exercism/metadata.json b/elixir/grade-school/.exercism/metadata.json new file mode 100644 index 0000000..7585414 --- /dev/null +++ b/elixir/grade-school/.exercism/metadata.json @@ -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} \ No newline at end of file diff --git a/elixir/grade-school/.formatter.exs b/elixir/grade-school/.formatter.exs new file mode 100644 index 0000000..d2cda26 --- /dev/null +++ b/elixir/grade-school/.formatter.exs @@ -0,0 +1,4 @@ +# Used by "mix format" +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/elixir/grade-school/.gitignore b/elixir/grade-school/.gitignore new file mode 100644 index 0000000..4c2cc24 --- /dev/null +++ b/elixir/grade-school/.gitignore @@ -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 + diff --git a/elixir/grade-school/HELP.md b/elixir/grade-school/HELP.md new file mode 100644 index 0000000..d26cce0 --- /dev/null +++ b/elixir/grade-school/HELP.md @@ -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/.exs:LINENUM` - runs only a single test, the test from `.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. \ No newline at end of file diff --git a/elixir/grade-school/README.md b/elixir/grade-school/README.md new file mode 100644 index 0000000..0a5215c --- /dev/null +++ b/elixir/grade-school/README.md @@ -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 \ No newline at end of file diff --git a/elixir/grade-school/lib/school.ex b/elixir/grade-school/lib/school.ex new file mode 100644 index 0000000..75caead --- /dev/null +++ b/elixir/grade-school/lib/school.ex @@ -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 diff --git a/elixir/grade-school/mix.exs b/elixir/grade-school/mix.exs new file mode 100644 index 0000000..6031b5f --- /dev/null +++ b/elixir/grade-school/mix.exs @@ -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 diff --git a/elixir/grade-school/test/school_test.exs b/elixir/grade-school/test/school_test.exs new file mode 100644 index 0000000..0dd9b22 --- /dev/null +++ b/elixir/grade-school/test/school_test.exs @@ -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 diff --git a/elixir/grade-school/test/test_helper.exs b/elixir/grade-school/test/test_helper.exs new file mode 100644 index 0000000..35fc5bf --- /dev/null +++ b/elixir/grade-school/test/test_helper.exs @@ -0,0 +1,2 @@ +ExUnit.start() +ExUnit.configure(exclude: :pending, trace: true)