From 8d532cc34bb9d2250e8529272ab3a8cebf002109 Mon Sep 17 00:00:00 2001 From: Erik Schierboom Date: Sun, 26 May 2024 18:38:24 +0200 Subject: [PATCH] Add `raindrops` exercise (#27) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add `raindrops` exercise * Sort tests to canonical order --------- Co-authored-by: AndrĂ¡s B Nagy <20251272+BNAndras@users.noreply.github.com> --- config.json | 8 ++ .../practice/raindrops/.docs/instructions.md | 24 +++++ .../practice/raindrops/.docs/introduction.md | 3 + .../practice/raindrops/.meta/config.json | 19 ++++ .../practice/raindrops/.meta/src/example.art | 9 ++ exercises/practice/raindrops/.meta/tests.toml | 64 +++++++++++++ .../practice/raindrops/src/raindrops.art | 3 + exercises/practice/raindrops/tester.art | 3 + .../raindrops/tests/test-raindrops.art | 94 +++++++++++++++++++ 9 files changed, 227 insertions(+) create mode 100644 exercises/practice/raindrops/.docs/instructions.md create mode 100644 exercises/practice/raindrops/.docs/introduction.md create mode 100644 exercises/practice/raindrops/.meta/config.json create mode 100644 exercises/practice/raindrops/.meta/src/example.art create mode 100644 exercises/practice/raindrops/.meta/tests.toml create mode 100644 exercises/practice/raindrops/src/raindrops.art create mode 100644 exercises/practice/raindrops/tester.art create mode 100644 exercises/practice/raindrops/tests/test-raindrops.art diff --git a/config.json b/config.json index 3ea1dfd..a0e30f7 100644 --- a/config.json +++ b/config.json @@ -100,6 +100,14 @@ "prerequisites": [], "difficulty": 2 }, + { + "slug": "raindrops", + "name": "Raindrops", + "uuid": "446fe3c2-ac91-4baa-b78f-e83def979eaf", + "practices": [], + "prerequisites": [], + "difficulty": 3 + }, { "slug": "high-scores", "name": "High Scores", diff --git a/exercises/practice/raindrops/.docs/instructions.md b/exercises/practice/raindrops/.docs/instructions.md new file mode 100644 index 0000000..df64410 --- /dev/null +++ b/exercises/practice/raindrops/.docs/instructions.md @@ -0,0 +1,24 @@ +# Instructions + +Your task is to convert a number into its corresponding raindrop sounds. + +If a given number: + +- is divisible by 3, add "Pling" to the result. +- is divisible by 5, add "Plang" to the result. +- is divisible by 7, add "Plong" to the result. +- **is not** divisible by 3, 5, or 7, the result should be the number as a string. + +## Examples + +- 28 is divisible by 7, but not 3 or 5, so the result would be `"Plong"`. +- 30 is divisible by 3 and 5, but not 7, so the result would be `"PlingPlang"`. +- 34 is not divisible by 3, 5, or 7, so the result would be `"34"`. + +~~~~exercism/note +A common way to test if one number is evenly divisible by another is to compare the [remainder][remainder] or [modulus][modulo] to zero. +Most languages provide operators or functions for one (or both) of these. + +[remainder]: https://exercism.org/docs/programming/operators/remainder +[modulo]: https://en.wikipedia.org/wiki/Modulo_operation +~~~~ diff --git a/exercises/practice/raindrops/.docs/introduction.md b/exercises/practice/raindrops/.docs/introduction.md new file mode 100644 index 0000000..ba12100 --- /dev/null +++ b/exercises/practice/raindrops/.docs/introduction.md @@ -0,0 +1,3 @@ +# Introduction + +Raindrops is a slightly more complex version of the FizzBuzz challenge, a classic interview question. diff --git a/exercises/practice/raindrops/.meta/config.json b/exercises/practice/raindrops/.meta/config.json new file mode 100644 index 0000000..659dca5 --- /dev/null +++ b/exercises/practice/raindrops/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "erikschierboom" + ], + "files": { + "solution": [ + "src/raindrops.art" + ], + "test": [ + "tests/test-raindrops.art" + ], + "example": [ + ".meta/src/example.art" + ] + }, + "blurb": "Convert a number into its corresponding raindrop sounds - Pling, Plang and Plong.", + "source": "A variation on FizzBuzz, a famous technical interview question that is intended to weed out potential candidates. That question is itself derived from Fizz Buzz, a popular children's game for teaching division.", + "source_url": "https://en.wikipedia.org/wiki/Fizz_buzz" +} diff --git a/exercises/practice/raindrops/.meta/src/example.art b/exercises/practice/raindrops/.meta/src/example.art new file mode 100644 index 0000000..93e5930 --- /dev/null +++ b/exercises/practice/raindrops/.meta/src/example.art @@ -0,0 +1,9 @@ +raindrops: $[n][ + sound: "" + switch zero? n%3 -> 'sound ++ "Pling" -> [] + switch zero? n%5 -> 'sound ++ "Plang" -> [] + switch zero? n%7 -> 'sound ++ "Plong" -> [] + switch empty? sound + -> to :string n + -> sound +] diff --git a/exercises/practice/raindrops/.meta/tests.toml b/exercises/practice/raindrops/.meta/tests.toml new file mode 100644 index 0000000..756d16c --- /dev/null +++ b/exercises/practice/raindrops/.meta/tests.toml @@ -0,0 +1,64 @@ +# 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. + +[1575d549-e502-46d4-a8e1-6b7bec6123d8] +description = "the sound for 1 is 1" + +[1f51a9f9-4895-4539-b182-d7b0a5ab2913] +description = "the sound for 3 is Pling" + +[2d9bfae5-2b21-4bcd-9629-c8c0e388f3e0] +description = "the sound for 5 is Plang" + +[d7e60daa-32ef-4c23-b688-2abff46c4806] +description = "the sound for 7 is Plong" + +[6bb4947b-a724-430c-923f-f0dc3d62e56a] +description = "the sound for 6 is Pling as it has a factor 3" + +[ce51e0e8-d9d4-446d-9949-96eac4458c2d] +description = "2 to the power 3 does not make a raindrop sound as 3 is the exponent not the base" + +[0dd66175-e3e2-47fc-8750-d01739856671] +description = "the sound for 9 is Pling as it has a factor 3" + +[022c44d3-2182-4471-95d7-c575af225c96] +description = "the sound for 10 is Plang as it has a factor 5" + +[37ab74db-fed3-40ff-b7b9-04acdfea8edf] +description = "the sound for 14 is Plong as it has a factor of 7" + +[31f92999-6afb-40ee-9aa4-6d15e3334d0f] +description = "the sound for 15 is PlingPlang as it has factors 3 and 5" + +[ff9bb95d-6361-4602-be2c-653fe5239b54] +description = "the sound for 21 is PlingPlong as it has factors 3 and 7" + +[d2e75317-b72e-40ab-8a64-6734a21dece1] +description = "the sound for 25 is Plang as it has a factor 5" + +[a09c4c58-c662-4e32-97fe-f1501ef7125c] +description = "the sound for 27 is Pling as it has a factor 3" + +[bdf061de-8564-4899-a843-14b48b722789] +description = "the sound for 35 is PlangPlong as it has factors 5 and 7" + +[c4680bee-69ba-439d-99b5-70c5fd1a7a83] +description = "the sound for 49 is Plong as it has a factor 7" + +[17f2bc9a-b65a-4d23-8ccd-266e8c271444] +description = "the sound for 52 is 52" + +[e46677ed-ff1a-419f-a740-5c713d2830e4] +description = "the sound for 105 is PlingPlangPlong as it has factors 3, 5 and 7" + +[13c6837a-0fcd-4b86-a0eb-20572f7deb0b] +description = "the sound for 3125 is Plang as it has a factor 5" diff --git a/exercises/practice/raindrops/src/raindrops.art b/exercises/practice/raindrops/src/raindrops.art new file mode 100644 index 0000000..65e9c77 --- /dev/null +++ b/exercises/practice/raindrops/src/raindrops.art @@ -0,0 +1,3 @@ +raindrops: function[n][ + panic "Please implement the raindrops function" +] diff --git a/exercises/practice/raindrops/tester.art b/exercises/practice/raindrops/tester.art new file mode 100644 index 0000000..80f4a8f --- /dev/null +++ b/exercises/practice/raindrops/tester.art @@ -0,0 +1,3 @@ +import {unitt}! + +runTests.failFast findTests "tests" diff --git a/exercises/practice/raindrops/tests/test-raindrops.art b/exercises/practice/raindrops/tests/test-raindrops.art new file mode 100644 index 0000000..1ab6d8d --- /dev/null +++ b/exercises/practice/raindrops/tests/test-raindrops.art @@ -0,0 +1,94 @@ +import {unitt}! +import {src/raindrops}! + +suite "Raindrops" [ + test "the sound of 1 is 1" [ + sound: raindrops 1 + assert -> "1" = sound + ] + + test.skip "the sound for 3 is Pling" [ + sound: raindrops 3 + assert -> "Pling" = sound + ] + + test.skip "the sound for 5 is Plang" [ + sound: raindrops 5 + assert -> "Plang" = sound + ] + + test.skip "the sound of 7 is Plong" [ + sound: raindrops 7 + assert -> "Plong" = sound + ] + + test.skip "the sound for 6 is Pling as it has a factor of 3" [ + sound: raindrops 6 + assert -> "Pling" = sound + ] + + test.skip "2 to the power 3 does not make a raindrop sound as 3 is the exponent not the base" [ + sound: raindrops 8 + assert -> "8" = sound + ] + + test.skip "the sound of 9 is Pling as it has a factor of 3" [ + sound: raindrops 9 + assert -> "Pling" = sound + ] + + test.skip "the sound for 10 is Plang as it has a factor of 5" [ + sound: raindrops 10 + assert -> "Plang" = sound + ] + + test.skip "the sound for 14 is Plong as it has a factor of 7" [ + sound: raindrops 14 + assert -> "Plong" = sound + ] + + test.skip "the sound for 15 is PlingPlang as it has factors 3 and 5" [ + sound: raindrops 15 + assert -> "PlingPlang" = sound + ] + + test.skip "the sound for 21 is PlingPlong as it has factors 3 and 7" [ + sound: raindrops 21 + assert -> "PlingPlong" = sound + ] + + test.skip "the sound for 25 is Plang as it has a factor of 5" [ + sound: raindrops 25 + assert -> "Plang" = sound + ] + + test.skip "the sound for 27 is Pling as it has a factor of 3" [ + sound: raindrops 27 + assert -> "Pling" = sound + ] + + test.skip "the sound for 35 is PlangPlong as it has factors 5 and 7" [ + sound: raindrops 35 + assert -> "PlangPlong" = sound + ] + + test.skip "the sound for 49 is Plong as it has a factor of 7" [ + sound: raindrops 49 + assert -> "Plong" = sound + ] + + test.skip "the sound for 52 is 52" [ + sound: raindrops 52 + assert -> "52" = sound + ] + + test.skip "the sound for 105 is PlingPlangPlong as it has factors 3, 5 and 7" [ + sound: raindrops 105 + assert -> "PlingPlangPlong" = sound + ] + + test.skip "the sound for 3125 is Plang as it has a factor of 5" [ + sound: raindrops 3125 + assert -> "Plang" = sound + ] +]