From bed0ebd267818e2e9a3d89719f70727f96f1a125 Mon Sep 17 00:00:00 2001 From: Kah Goh Date: Wed, 31 Jan 2024 21:30:29 +0800 Subject: [PATCH 1/2] Add Raindrops practice exercise --- config.json | 8 +++ .../practice/raindrops/.docs/instructions.md | 24 +++++++ .../practice/raindrops/.docs/introduction.md | 3 + .../practice/raindrops/.meta/config.json | 19 ++++++ .../practice/raindrops/.meta/example.lfe | 27 ++++++++ exercises/practice/raindrops/.meta/tests.toml | 64 +++++++++++++++++++ exercises/practice/raindrops/Makefile | 21 ++++++ exercises/practice/raindrops/rebar.config | 11 ++++ exercises/practice/raindrops/rebar.lock | 8 +++ .../practice/raindrops/src/raindrops.app.src | 11 ++++ .../practice/raindrops/src/raindrops.lfe | 4 ++ .../raindrops/test/raindrops-tests.lfe | 56 ++++++++++++++++ 12 files changed, 256 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/example.lfe create mode 100644 exercises/practice/raindrops/.meta/tests.toml create mode 100644 exercises/practice/raindrops/Makefile create mode 100644 exercises/practice/raindrops/rebar.config create mode 100644 exercises/practice/raindrops/rebar.lock create mode 100644 exercises/practice/raindrops/src/raindrops.app.src create mode 100644 exercises/practice/raindrops/src/raindrops.lfe create mode 100644 exercises/practice/raindrops/test/raindrops-tests.lfe diff --git a/config.json b/config.json index eb01d03..cf16a7b 100644 --- a/config.json +++ b/config.json @@ -311,6 +311,14 @@ "practices": [], "prerequisites": [], "difficulty": 1 + }, + { + "slug": "raindrops", + "name": "Raindrops", + "uuid": "d53f769b-b294-4fb0-a336-881549a15a94", + "practices": [], + "prerequisites": [], + "difficulty": 1 } ] }, 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..1da624e --- /dev/null +++ b/exercises/practice/raindrops/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "kahgoh" + ], + "files": { + "solution": [ + "src/raindrops.lfe" + ], + "test": [ + "test/raindrops-tests.lfe" + ], + "example": [ + ".meta/example.lfe" + ] + }, + "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/example.lfe b/exercises/practice/raindrops/.meta/example.lfe new file mode 100644 index 0000000..37ac929 --- /dev/null +++ b/exercises/practice/raindrops/.meta/example.lfe @@ -0,0 +1,27 @@ +(defmodule raindrops + (export (convert 1))) + +(defun fill-string + (("" value) (integer_to_list value)) + ((content _value) content) +) + +(defun check-sound + ((word 0) word) + ((_word _value) "") +) + +(defun convert + ((value) + (fill-string + (lists:concat + (list + (check-sound "Pling" (rem value 3)) + (check-sound "Plang" (rem value 5)) + (check-sound "Plong" (rem value 7)) + ) + ) + value + ) + ) +) 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/Makefile b/exercises/practice/raindrops/Makefile new file mode 100644 index 0000000..fbb5a7d --- /dev/null +++ b/exercises/practice/raindrops/Makefile @@ -0,0 +1,21 @@ +ERL := $(shell which erl) +REBAR3 := $(shell which rebar3) + +null := +space := $(null) # +comma := , + +ifeq ($(ERL),) + $(error Can't find Erlang executable 'erl') +else ifeq ($(REBAR3),) + $(error Can't find rebar3) +endif + +compile: ; $(REBAR3) compile + +clean: ; $(REBAR3) clean + +.PHONY: test +test: + $(REBAR3) eunit \ + -m $(subst $(space),$(comma),$(basename $(notdir $(wildcard test/*.lfe)))) diff --git a/exercises/practice/raindrops/rebar.config b/exercises/practice/raindrops/rebar.config new file mode 100644 index 0000000..d53487a --- /dev/null +++ b/exercises/practice/raindrops/rebar.config @@ -0,0 +1,11 @@ +{plugins, [{rebar3_lfe, "0.4.3"}]}. + +{provider_hooks, [{post, [{compile, {lfe, compile}}]}]}. + +{deps, [{lfe, "2.1.1"}]}. + +{profiles, + [{test, + [{eunit_compile_opts, [{src_dirs, ["src", "test"]}]}, + {deps, + [{ltest, "0.13.3"}]}]}]}. diff --git a/exercises/practice/raindrops/rebar.lock b/exercises/practice/raindrops/rebar.lock new file mode 100644 index 0000000..d5a6b3b --- /dev/null +++ b/exercises/practice/raindrops/rebar.lock @@ -0,0 +1,8 @@ +{"1.2.0", +[{<<"lfe">>,{pkg,<<"lfe">>,<<"2.1.1">>},0}]}. +[ +{pkg_hash,[ + {<<"lfe">>, <<"4A888B26172D198DC7A5AFEB897E8248AF7D56E1638D9C8249AAF933AE811B96">>}]}, +{pkg_hash_ext,[ + {<<"lfe">>, <<"C484D3B655D40DED58BC41B17B22F173711C681BF36063A234A9BAA9506947E1">>}]} +]. diff --git a/exercises/practice/raindrops/src/raindrops.app.src b/exercises/practice/raindrops/src/raindrops.app.src new file mode 100644 index 0000000..7a153a1 --- /dev/null +++ b/exercises/practice/raindrops/src/raindrops.app.src @@ -0,0 +1,11 @@ +%% -*- erlang -*- +{application, 'raindrops', + [{description, "Convert a number into its corresponding raindrop sounds - Pling, Plang and Plong."}, + {vsn, "0.0.1"}, + {modules, + ['raindrops']}, + {registered, []}, + {applications, + [kernel, stdlib]}, + {included_applications, []}, + {env, []}]}. diff --git a/exercises/practice/raindrops/src/raindrops.lfe b/exercises/practice/raindrops/src/raindrops.lfe new file mode 100644 index 0000000..9617f2a --- /dev/null +++ b/exercises/practice/raindrops/src/raindrops.lfe @@ -0,0 +1,4 @@ +(defmodule raindrops + (export (convert 1))) + + ; Write your code for the "Raindrops" exercise here diff --git a/exercises/practice/raindrops/test/raindrops-tests.lfe b/exercises/practice/raindrops/test/raindrops-tests.lfe new file mode 100644 index 0000000..cd60e23 --- /dev/null +++ b/exercises/practice/raindrops/test/raindrops-tests.lfe @@ -0,0 +1,56 @@ +(defmodule raindrops-tests + (behaviour ltest-unit) + (export all)) + +(include-lib "ltest/include/ltest-macros.lfe") + +(deftest sound-for-1-is-1 + (is-equal "1" (raindrops:convert 1))) + +(deftest sound-for-3-is-pling + (is-equal "Pling" (raindrops:convert 3))) + +(deftest sound-for-5-is-plang + (is-equal "Plang" (raindrops:convert 5))) + +(deftest sound-for-7-is-plong + (is-equal "Plong" (raindrops:convert 7))) + +(deftest sound-for-6-is-pling + (is-equal "Pling" (raindrops:convert 6))) + +(deftest 2-to-the-power-3-no-raindrop-sound + (is-equal "8" (raindrops:convert 8))) + +(deftest sound-for-9-is-pling + (is-equal "Pling" (raindrops:convert 9))) + +(deftest sound-for-10-is-plang + (is-equal "Plang" (raindrops:convert 10))) + +(deftest sound-for-15-is-plingplang + (is-equal "PlingPlang" (raindrops:convert 15))) + +(deftest sound-for-21-is-plingplong + (is-equal "PlingPlong" (raindrops:convert 21))) + +(deftest sound-for-25-is-plang + (is-equal "Plang" (raindrops:convert 25))) + +(deftest sound-for-27-is-pling + (is-equal "Pling" (raindrops:convert 27))) + +(deftest sound-for-35-is-plangplong + (is-equal "PlangPlong" (raindrops:convert 35))) + +(deftest sound-for-49-is-plong + (is-equal "Plong" (raindrops:convert 49))) + +(deftest sound-for-52-no-raindrop-sound + (is-equal "52" (raindrops:convert 52))) + +(deftest sound-for-105-is-PlingPlangPlong + (is-equal "PlingPlangPlong" (raindrops:convert 105))) + +(deftest sound-for-3125-is-plang + (is-equal "Plang" (raindrops:convert 3125))) From 0b44878070e260688fd60270aba09b6c4af10b29 Mon Sep 17 00:00:00 2001 From: Kah Goh Date: Wed, 31 Jan 2024 22:27:17 +0800 Subject: [PATCH 2/2] Apply suggestion for exercises/practice/raindrops/src/raindrops.lfe Co-authored-by: Erik Schierboom --- exercises/practice/raindrops/src/raindrops.lfe | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/practice/raindrops/src/raindrops.lfe b/exercises/practice/raindrops/src/raindrops.lfe index 9617f2a..e481ba0 100644 --- a/exercises/practice/raindrops/src/raindrops.lfe +++ b/exercises/practice/raindrops/src/raindrops.lfe @@ -1,4 +1,4 @@ (defmodule raindrops (export (convert 1))) - ; Write your code for the "Raindrops" exercise here + ; Please implement the `convert` function.