From 4e427396821417860dad9d2e44a60c558bece940 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Geron?= Date: Sun, 29 Sep 2024 18:46:24 +1300 Subject: [PATCH] Add kindergarten-garden exercise (#119) * Add kindergarten-garden exercise * Pattern matching on student --- config.json | 8 + .../kindergarten-garden/.docs/instructions.md | 56 +++++ .../kindergarten-garden/.docs/introduction.md | 6 + .../kindergarten-garden/.meta/Example.roc | 39 ++++ .../kindergarten-garden/.meta/config.json | 19 ++ .../kindergarten-garden/.meta/template.j2 | 30 +++ .../kindergarten-garden/.meta/tests.toml | 61 ++++++ .../KindergartenGarden.roc | 8 + .../kindergarten-garden-test.roc | 192 ++++++++++++++++++ 9 files changed, 419 insertions(+) create mode 100644 exercises/practice/kindergarten-garden/.docs/instructions.md create mode 100644 exercises/practice/kindergarten-garden/.docs/introduction.md create mode 100644 exercises/practice/kindergarten-garden/.meta/Example.roc create mode 100644 exercises/practice/kindergarten-garden/.meta/config.json create mode 100644 exercises/practice/kindergarten-garden/.meta/template.j2 create mode 100644 exercises/practice/kindergarten-garden/.meta/tests.toml create mode 100644 exercises/practice/kindergarten-garden/KindergartenGarden.roc create mode 100644 exercises/practice/kindergarten-garden/kindergarten-garden-test.roc diff --git a/config.json b/config.json index 93c6f62..2220463 100644 --- a/config.json +++ b/config.json @@ -143,6 +143,14 @@ "prerequisites": [], "difficulty": 2 }, + { + "slug": "kindergarten-garden", + "name": "Kindergarten Garden", + "uuid": "5c3e5171-d541-47fb-9cf3-a3965f5b3118", + "practices": [], + "prerequisites": [], + "difficulty": 2 + }, { "slug": "micro-blog", "name": "Micro Blog", diff --git a/exercises/practice/kindergarten-garden/.docs/instructions.md b/exercises/practice/kindergarten-garden/.docs/instructions.md new file mode 100644 index 0000000..6fe11a5 --- /dev/null +++ b/exercises/practice/kindergarten-garden/.docs/instructions.md @@ -0,0 +1,56 @@ +# 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 diff --git a/exercises/practice/kindergarten-garden/.docs/introduction.md b/exercises/practice/kindergarten-garden/.docs/introduction.md new file mode 100644 index 0000000..5ad97d2 --- /dev/null +++ b/exercises/practice/kindergarten-garden/.docs/introduction.md @@ -0,0 +1,6 @@ +# 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. diff --git a/exercises/practice/kindergarten-garden/.meta/Example.roc b/exercises/practice/kindergarten-garden/.meta/Example.roc new file mode 100644 index 0000000..09d5c3f --- /dev/null +++ b/exercises/practice/kindergarten-garden/.meta/Example.roc @@ -0,0 +1,39 @@ +module [plants] + +Student : [Alice, Bob, Charlie, David, Eve, Fred, Ginny, Harriet, Ileana, Joseph, Kincaid, Larry] +Plant : [Grass, Clover, Radishes, Violets] + +studentIndex : Student -> U64 +studentIndex = \student -> + when student is + Alice -> 0 + Bob -> 1 + Charlie -> 2 + David -> 3 + Eve -> 4 + Fred -> 5 + Ginny -> 6 + Harriet -> 7 + Ileana -> 8 + Joseph -> 9 + Kincaid -> 10 + Larry -> 11 + +plants : Str, Student -> Result (List Plant) _ +plants = \diagram, student -> + startIndex = 2 * studentIndex student + grid = + diagram + |> Str.trim + |> Str.split "\n" + |> List.map \row -> + row |> Str.trim |> Str.toUtf8 + [(0, 0), (0, 1), (1, 0), (1, 1)] + |> List.mapTry \(row, column) -> + plant = grid |> List.get? row |> List.get? (startIndex + column) + when plant is + 'G' -> Ok Grass + 'C' -> Ok Clover + 'R' -> Ok Radishes + 'V' -> Ok Violets + _ -> Err (UnknownPlant plant) diff --git a/exercises/practice/kindergarten-garden/.meta/config.json b/exercises/practice/kindergarten-garden/.meta/config.json new file mode 100644 index 0000000..388d034 --- /dev/null +++ b/exercises/practice/kindergarten-garden/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "ageron" + ], + "files": { + "solution": [ + "KindergartenGarden.roc" + ], + "test": [ + "kindergarten-garden-test.roc" + ], + "example": [ + ".meta/Example.roc" + ] + }, + "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/exercises/practice/kindergarten-garden/.meta/template.j2 b/exercises/practice/kindergarten-garden/.meta/template.j2 new file mode 100644 index 0000000..40f0318 --- /dev/null +++ b/exercises/practice/kindergarten-garden/.meta/template.j2 @@ -0,0 +1,30 @@ +{%- import "generator_macros.j2" as macros with context -%} +{{ macros.canonical_ref() }} +{{ macros.header() }} + +import {{ exercise | to_pascal }} exposing [plants] + +{% for supercase in cases %} +### +### {{ supercase["description"] }} +### + +{% for case in supercase["cases"] -%} +{%- if case["cases"] %} +## {{ case["description"] }} +{%- set subcases = case["cases"] %} +{%- else %} +{%- set subcases = [case] %} +{%- endif %} + +{% for subcase in subcases -%} +# {{ subcase["description"] }} +expect + diagram = {{ subcase["input"]["diagram"] | to_roc_multiline_string | indent(8) }} + result = diagram |> {{ subcase["property"] | to_camel }} {{ subcase["input"]["student"] | to_pascal }} + result == Ok [{% for plant in subcase["expected"] %}{{ plant | to_pascal }}, {% endfor %}] + +{% endfor %} + +{% endfor %} +{% endfor %} \ No newline at end of file diff --git a/exercises/practice/kindergarten-garden/.meta/tests.toml b/exercises/practice/kindergarten-garden/.meta/tests.toml new file mode 100644 index 0000000..0cdd9ad --- /dev/null +++ b/exercises/practice/kindergarten-garden/.meta/tests.toml @@ -0,0 +1,61 @@ +# This is an auto-generated file. +# +# Regenerating this file via `configlet sync` will: +# - Recreate every `description` key/value pair +# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications +# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) +# - Preserve any other key/value pair +# +# As user-added comments (using the # character) will be removed when this file +# is regenerated, comments can be added via a `comment` key. + +[1fc316ed-17ab-4fba-88ef-3ae78296b692] +description = "partial garden -> garden with single student" + +[acd19dc1-2200-4317-bc2a-08f021276b40] +description = "partial garden -> different garden with single student" + +[c376fcc8-349c-446c-94b0-903947315757] +description = "partial garden -> garden with two students" + +[2d620f45-9617-4924-9d27-751c80d17db9] +description = "partial garden -> multiple students for the same garden with three students -> second student's garden" + +[57712331-4896-4364-89f8-576421d69c44] +description = "partial garden -> multiple students for the same garden with three students -> third student's garden" + +[149b4290-58e1-40f2-8ae4-8b87c46e765b] +description = "full garden -> for Alice, first student's garden" + +[ba25dbbc-10bd-4a37-b18e-f89ecd098a5e] +description = "full garden -> for Bob, second student's garden" + +[566b621b-f18e-4c5f-873e-be30544b838c] +description = "full garden -> for Charlie" + +[3ad3df57-dd98-46fc-9269-1877abf612aa] +description = "full garden -> for David" + +[0f0a55d1-9710-46ed-a0eb-399ba8c72db2] +description = "full garden -> for Eve" + +[a7e80c90-b140-4ea1-aee3-f4625365c9a4] +description = "full garden -> for Fred" + +[9d94b273-2933-471b-86e8-dba68694c615] +description = "full garden -> for Ginny" + +[f55bc6c2-ade8-4844-87c4-87196f1b7258] +description = "full garden -> for Harriet" + +[759070a3-1bb1-4dd4-be2c-7cce1d7679ae] +description = "full garden -> for Ileana" + +[78578123-2755-4d4a-9c7d-e985b8dda1c6] +description = "full garden -> for Joseph" + +[6bb66df7-f433-41ab-aec2-3ead6e99f65b] +description = "full garden -> for Kincaid, second to last student's garden" + +[d7edec11-6488-418a-94e6-ed509e0fa7eb] +description = "full garden -> for Larry, last student's garden" diff --git a/exercises/practice/kindergarten-garden/KindergartenGarden.roc b/exercises/practice/kindergarten-garden/KindergartenGarden.roc new file mode 100644 index 0000000..5f00b2f --- /dev/null +++ b/exercises/practice/kindergarten-garden/KindergartenGarden.roc @@ -0,0 +1,8 @@ +module [plants] + +Student : [Alice, Bob, Charlie, David, Eve, Fred, Ginny, Harriet, Ileana, Joseph, Kincaid, Larry] +Plant : [Grass, Clover, Radishes, Violets] + +plants : Str, Student -> Result (List Plant) _ +plants = \diagram, student -> + crash "Please implement the 'plants' function" diff --git a/exercises/practice/kindergarten-garden/kindergarten-garden-test.roc b/exercises/practice/kindergarten-garden/kindergarten-garden-test.roc new file mode 100644 index 0000000..6451569 --- /dev/null +++ b/exercises/practice/kindergarten-garden/kindergarten-garden-test.roc @@ -0,0 +1,192 @@ +# These tests are auto-generated with test data from: +# https://github.com/exercism/problem-specifications/tree/main/exercises/kindergarten-garden/canonical-data.json +# File last updated on 2024-09-26 +app [main] { + pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.15.0/SlwdbJ-3GR7uBWQo6zlmYWNYOxnvo8r6YABXD-45UOw.tar.br", +} + +main = + Task.ok {} + +import KindergartenGarden exposing [plants] + +### +### partial garden +### + +# garden with single student +expect + diagram = + """ + RC + GG + """ + result = diagram |> plants Alice + result == Ok [Radishes, Clover, Grass, Grass] + +# different garden with single student +expect + diagram = + """ + VC + RC + """ + result = diagram |> plants Alice + result == Ok [Violets, Clover, Radishes, Clover] + +# garden with two students +expect + diagram = + """ + VVCG + VVRC + """ + result = diagram |> plants Bob + result == Ok [Clover, Grass, Radishes, Clover] + +## multiple students for the same garden with three students + +# second student's garden +expect + diagram = + """ + VVCCGG + VVCCGG + """ + result = diagram |> plants Bob + result == Ok [Clover, Clover, Clover, Clover] + +# third student's garden +expect + diagram = + """ + VVCCGG + VVCCGG + """ + result = diagram |> plants Charlie + result == Ok [Grass, Grass, Grass, Grass] + +### +### full garden +### + +# for Alice, first student's garden +expect + diagram = + """ + VRCGVVRVCGGCCGVRGCVCGCGV + VRCCCGCRRGVCGCRVVCVGCGCV + """ + result = diagram |> plants Alice + result == Ok [Violets, Radishes, Violets, Radishes] + +# for Bob, second student's garden +expect + diagram = + """ + VRCGVVRVCGGCCGVRGCVCGCGV + VRCCCGCRRGVCGCRVVCVGCGCV + """ + result = diagram |> plants Bob + result == Ok [Clover, Grass, Clover, Clover] + +# for Charlie +expect + diagram = + """ + VRCGVVRVCGGCCGVRGCVCGCGV + VRCCCGCRRGVCGCRVVCVGCGCV + """ + result = diagram |> plants Charlie + result == Ok [Violets, Violets, Clover, Grass] + +# for David +expect + diagram = + """ + VRCGVVRVCGGCCGVRGCVCGCGV + VRCCCGCRRGVCGCRVVCVGCGCV + """ + result = diagram |> plants David + result == Ok [Radishes, Violets, Clover, Radishes] + +# for Eve +expect + diagram = + """ + VRCGVVRVCGGCCGVRGCVCGCGV + VRCCCGCRRGVCGCRVVCVGCGCV + """ + result = diagram |> plants Eve + result == Ok [Clover, Grass, Radishes, Grass] + +# for Fred +expect + diagram = + """ + VRCGVVRVCGGCCGVRGCVCGCGV + VRCCCGCRRGVCGCRVVCVGCGCV + """ + result = diagram |> plants Fred + result == Ok [Grass, Clover, Violets, Clover] + +# for Ginny +expect + diagram = + """ + VRCGVVRVCGGCCGVRGCVCGCGV + VRCCCGCRRGVCGCRVVCVGCGCV + """ + result = diagram |> plants Ginny + result == Ok [Clover, Grass, Grass, Clover] + +# for Harriet +expect + diagram = + """ + VRCGVVRVCGGCCGVRGCVCGCGV + VRCCCGCRRGVCGCRVVCVGCGCV + """ + result = diagram |> plants Harriet + result == Ok [Violets, Radishes, Radishes, Violets] + +# for Ileana +expect + diagram = + """ + VRCGVVRVCGGCCGVRGCVCGCGV + VRCCCGCRRGVCGCRVVCVGCGCV + """ + result = diagram |> plants Ileana + result == Ok [Grass, Clover, Violets, Clover] + +# for Joseph +expect + diagram = + """ + VRCGVVRVCGGCCGVRGCVCGCGV + VRCCCGCRRGVCGCRVVCVGCGCV + """ + result = diagram |> plants Joseph + result == Ok [Violets, Clover, Violets, Grass] + +# for Kincaid, second to last student's garden +expect + diagram = + """ + VRCGVVRVCGGCCGVRGCVCGCGV + VRCCCGCRRGVCGCRVVCVGCGCV + """ + result = diagram |> plants Kincaid + result == Ok [Grass, Clover, Clover, Grass] + +# for Larry, last student's garden +expect + diagram = + """ + VRCGVVRVCGGCCGVRGCVCGCGV + VRCCCGCRRGVCGCRVVCVGCGCV + """ + result = diagram |> plants Larry + result == Ok [Grass, Violets, Clover, Violets] +