From 22d545194e2c06cec6a943ce5bf5bbe29a9391e9 Mon Sep 17 00:00:00 2001 From: Danylo Negrienko Date: Sat, 6 Jul 2024 20:06:28 -0400 Subject: [PATCH] kindergarten-garden --- .../kindergarten-garden/.exercism/config.json | 29 ++++++ .../.exercism/metadata.json | 1 + elixir/kindergarten-garden/.formatter.exs | 4 + elixir/kindergarten-garden/.gitignore | 24 +++++ elixir/kindergarten-garden/HELP.md | 75 ++++++++++++++++ elixir/kindergarten-garden/README.md | 89 ++++++++++++++++++ elixir/kindergarten-garden/lib/garden.ex | 42 +++++++++ elixir/kindergarten-garden/mix.exs | 28 ++++++ .../kindergarten-garden/test/garden_test.exs | 90 +++++++++++++++++++ .../kindergarten-garden/test/test_helper.exs | 2 + 10 files changed, 384 insertions(+) create mode 100644 elixir/kindergarten-garden/.exercism/config.json create mode 100644 elixir/kindergarten-garden/.exercism/metadata.json create mode 100644 elixir/kindergarten-garden/.formatter.exs create mode 100644 elixir/kindergarten-garden/.gitignore create mode 100644 elixir/kindergarten-garden/HELP.md create mode 100644 elixir/kindergarten-garden/README.md create mode 100644 elixir/kindergarten-garden/lib/garden.ex create mode 100644 elixir/kindergarten-garden/mix.exs create mode 100644 elixir/kindergarten-garden/test/garden_test.exs create mode 100644 elixir/kindergarten-garden/test/test_helper.exs diff --git a/elixir/kindergarten-garden/.exercism/config.json b/elixir/kindergarten-garden/.exercism/config.json new file mode 100644 index 0000000..d89b36b --- /dev/null +++ b/elixir/kindergarten-garden/.exercism/config.json @@ -0,0 +1,29 @@ +{ + "authors": [ + "devonestes" + ], + "contributors": [ + "angelikatyborska", + "Cohen-Carlisle", + "jwworth", + "lpil", + "nathanielknight", + "neenjaw", + "parkerl", + "sotojuan" + ], + "files": { + "solution": [ + "lib/garden.ex" + ], + "test": [ + "test/garden_test.exs" + ], + "example": [ + ".meta/example.ex" + ] + }, + "blurb": "Given a diagram, determine which plants each child in the kindergarten class is responsible for.", + "source": "Exercise by the JumpstartLab team for students at The Turing School of Software and Design.", + "source_url": "https://turing.edu" +} diff --git a/elixir/kindergarten-garden/.exercism/metadata.json b/elixir/kindergarten-garden/.exercism/metadata.json new file mode 100644 index 0000000..5d144aa --- /dev/null +++ b/elixir/kindergarten-garden/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"elixir","exercise":"kindergarten-garden","id":"9527c47ddff04eb9a3996d116c972a94","url":"https://exercism.org/tracks/elixir/exercises/kindergarten-garden","handle":"negrienko","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/elixir/kindergarten-garden/.formatter.exs b/elixir/kindergarten-garden/.formatter.exs new file mode 100644 index 0000000..d2cda26 --- /dev/null +++ b/elixir/kindergarten-garden/.formatter.exs @@ -0,0 +1,4 @@ +# Used by "mix format" +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/elixir/kindergarten-garden/.gitignore b/elixir/kindergarten-garden/.gitignore new file mode 100644 index 0000000..9dc8d60 --- /dev/null +++ b/elixir/kindergarten-garden/.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"). +kindergarten_garden-*.tar + diff --git a/elixir/kindergarten-garden/HELP.md b/elixir/kindergarten-garden/HELP.md new file mode 100644 index 0000000..ac5d454 --- /dev/null +++ b/elixir/kindergarten-garden/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/garden.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/kindergarten-garden/README.md b/elixir/kindergarten-garden/README.md new file mode 100644 index 0000000..1be4836 --- /dev/null +++ b/elixir/kindergarten-garden/README.md @@ -0,0 +1,89 @@ +# Kindergarten Garden + +Welcome to Kindergarten Garden on Exercism's Elixir Track. +If you need help running the tests or submitting your code, check out `HELP.md`. + +## Introduction + +The kindergarten class is learning about growing plants. +The teacher thought it would be a good idea to give the class seeds to plant and grow in the dirt. +To this end, the children have put little cups along the window sills and planted one type of plant in each cup. +The children got to pick their favorites from four available types of seeds: grass, clover, radishes, and violets. + +## Instructions + +Your task is to, given a diagram, determine which plants each child in the kindergarten class is responsible for. + +There are 12 children in the class: + +- Alice, Bob, Charlie, David, Eve, Fred, Ginny, Harriet, Ileana, Joseph, Kincaid, and Larry. + +Four different types of seeds are planted: + +| Plant | Diagram encoding | +| ------ | ---------------- | +| Grass | G | +| Clover | C | +| Radish | R | +| Violet | V | + +Each child gets four cups, two on each row: + +```text +[window][window][window] +........................ # each dot represents a cup +........................ +``` + +Their teacher assigns cups to the children alphabetically by their names, which means that Alice comes first and Larry comes last. + +Here is an example diagram representing Alice's plants: + +```text +[window][window][window] +VR...................... +RG...................... +``` + +In the first row, nearest the windows, she has a violet and a radish. +In the second row she has a radish and some grass. + +Your program will be given the plants from left-to-right starting with the row nearest the windows. +From this, it should be able to determine which plants belong to each student. + +For example, if it's told that the garden looks like so: + +```text +[window][window][window] +VRCGVVRVCGGCCGVRGCVCGCGV +VRCCCGCRRGVCGCRVVCVGCGCV +``` + +Then if asked for Alice's plants, it should provide: + +- Violets, radishes, violets, radishes + +While asking for Bob's plants would yield: + +- Clover, grass, clover, clover + +## Source + +### Created by + +- @devonestes + +### Contributed to by + +- @angelikatyborska +- @Cohen-Carlisle +- @jwworth +- @lpil +- @nathanielknight +- @neenjaw +- @parkerl +- @sotojuan + +### Based on + +Exercise by the JumpstartLab team for students at The Turing School of Software and Design. - https://turing.edu \ No newline at end of file diff --git a/elixir/kindergarten-garden/lib/garden.ex b/elixir/kindergarten-garden/lib/garden.ex new file mode 100644 index 0000000..1170bc7 --- /dev/null +++ b/elixir/kindergarten-garden/lib/garden.ex @@ -0,0 +1,42 @@ +defmodule Garden do + @children ~w(alice bob charlie david eve fred ginny harriet ileana joseph kincaid larry)a + @plant_codes ~w(G C R V) + @plants %{ + "G" => :grass, + "C" => :clover, + "R" => :radishes, + "V" => :violets + } + + defguardp is_plant_code(char) when char in @plant_codes + defp plant(char) when is_plant_code(char), do: Map.get(@plants, char) + + defp student_plants({name, position}, info_string) do + plants = info_string + |> String.split("\n") + |> Enum.map_join(&String.slice(&1, position * 2, 2)) + |> String.graphemes() + |> Enum.map(&plant/1) + |> List.to_tuple() + + {name, plants} + end + + @doc """ + Accepts a string representing the arrangement of cups on a windowsill and a + list with names of students in the class. The student names list does not + have to be in alphabetical order. + + It decodes that string into the various gardens for each student and returns + that information in a map. + """ + + @spec info(String.t(), list) :: map + def info(info_string, student_names \\ @children) do + student_names + |> Enum.sort() + |> Enum.with_index() + |> Enum.map(&student_plants(&1, info_string)) + |> Enum.into(%{}) + end +end diff --git a/elixir/kindergarten-garden/mix.exs b/elixir/kindergarten-garden/mix.exs new file mode 100644 index 0000000..260aafc --- /dev/null +++ b/elixir/kindergarten-garden/mix.exs @@ -0,0 +1,28 @@ +defmodule Garden.MixProject do + use Mix.Project + + def project do + [ + app: :garden, + 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/kindergarten-garden/test/garden_test.exs b/elixir/kindergarten-garden/test/garden_test.exs new file mode 100644 index 0000000..6858830 --- /dev/null +++ b/elixir/kindergarten-garden/test/garden_test.exs @@ -0,0 +1,90 @@ +defmodule GardenTest do + use ExUnit.Case + + test "gets the garden for Alice with just her plants" do + garden_info = Garden.info("RC\nGG") + assert garden_info.alice == {:radishes, :clover, :grass, :grass} + end + + test "gets another garden for Alice with just her plants" do + garden_info = Garden.info("VC\nRC") + assert garden_info.alice == {:violets, :clover, :radishes, :clover} + end + + test "returns an empty tuple if the child has no plants" do + garden_info = Garden.info("VC\nRC") + assert garden_info.bob == {} + end + + test "gets the garden for Bob" do + garden_info = Garden.info("VVCG\nVVRC") + assert garden_info.bob == {:clover, :grass, :radishes, :clover} + end + + test "plants are assigned in alphabetical order" do + garden_info = Garden.info("VVCC\nGGRR", [:bob, :alice]) + assert garden_info.alice == {:violets, :violets, :grass, :grass} + assert garden_info.bob == {:clover, :clover, :radishes, :radishes} + end + + test "a garden for 3 students" do + garden_info = Garden.info("VVCCGG\nVVCCGG") + assert garden_info.alice == {:violets, :violets, :violets, :violets} + assert garden_info.bob == {:clover, :clover, :clover, :clover} + assert garden_info.charlie == {:grass, :grass, :grass, :grass} + end + + test "gets the garden for all students" do + garden_info = Garden.info("VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV") + assert garden_info.alice == {:violets, :radishes, :violets, :radishes} + assert garden_info.bob == {:clover, :grass, :clover, :clover} + assert garden_info.charlie == {:violets, :violets, :clover, :grass} + assert garden_info.david == {:radishes, :violets, :clover, :radishes} + assert garden_info.eve == {:clover, :grass, :radishes, :grass} + assert garden_info.fred == {:grass, :clover, :violets, :clover} + assert garden_info.ginny == {:clover, :grass, :grass, :clover} + assert garden_info.harriet == {:violets, :radishes, :radishes, :violets} + assert garden_info.ileana == {:grass, :clover, :violets, :clover} + assert garden_info.joseph == {:violets, :clover, :violets, :grass} + assert garden_info.kincaid == {:grass, :clover, :clover, :grass} + assert garden_info.larry == {:grass, :violets, :clover, :violets} + end + + test "accepts custom child names" do + garden_info = Garden.info("VC\nRC", [:nate, :maggie]) + assert garden_info.maggie == {:violets, :clover, :radishes, :clover} + assert garden_info.nate == {} + end + + test "gets the garden for all students with custom child names" do + names = [ + :maggie, + :nate, + :xander, + :ophelia, + :pete, + :reggie, + :sylvia, + :tanner, + :ursula, + :victor, + :winnie, + :ynold + ] + + garden_string = "VRCGVVRVCGGCCGVRGCVCGCGV\nVRCCCGCRRGVCGCRVVCVGCGCV" + garden_info = Garden.info(garden_string, names) + assert garden_info.maggie == {:violets, :radishes, :violets, :radishes} + assert garden_info.nate == {:clover, :grass, :clover, :clover} + assert garden_info.ophelia == {:violets, :violets, :clover, :grass} + assert garden_info.pete == {:radishes, :violets, :clover, :radishes} + assert garden_info.reggie == {:clover, :grass, :radishes, :grass} + assert garden_info.sylvia == {:grass, :clover, :violets, :clover} + assert garden_info.tanner == {:clover, :grass, :grass, :clover} + assert garden_info.ursula == {:violets, :radishes, :radishes, :violets} + assert garden_info.victor == {:grass, :clover, :violets, :clover} + assert garden_info.winnie == {:violets, :clover, :violets, :grass} + assert garden_info.xander == {:grass, :clover, :clover, :grass} + assert garden_info.ynold == {:grass, :violets, :clover, :violets} + end +end diff --git a/elixir/kindergarten-garden/test/test_helper.exs b/elixir/kindergarten-garden/test/test_helper.exs new file mode 100644 index 0000000..35fc5bf --- /dev/null +++ b/elixir/kindergarten-garden/test/test_helper.exs @@ -0,0 +1,2 @@ +ExUnit.start() +ExUnit.configure(exclude: :pending, trace: true)