From e68f5f51ecaef4074b436006cc2e1f2524918546 Mon Sep 17 00:00:00 2001 From: Kah Goh Date: Fri, 16 Feb 2024 19:11:56 +0800 Subject: [PATCH] Add Roman numerals practice exercise (#193) --- config.json | 8 ++ .../roman-numerals/.docs/instructions.md | 12 +++ .../roman-numerals/.docs/introduction.md | 59 +++++++++++++ .../practice/roman-numerals/.meta/config.json | 22 +++++ .../practice/roman-numerals/.meta/example.lfe | 29 ++++++ .../practice/roman-numerals/.meta/tests.toml | 88 +++++++++++++++++++ exercises/practice/roman-numerals/Makefile | 21 +++++ .../practice/roman-numerals/rebar.config | 11 +++ exercises/practice/roman-numerals/rebar.lock | 8 ++ .../roman-numerals/src/roman-numerals.app.src | 11 +++ .../roman-numerals/src/roman-numerals.lfe | 5 ++ .../test/roman-numerals-tests.lfe | 83 +++++++++++++++++ 12 files changed, 357 insertions(+) create mode 100644 exercises/practice/roman-numerals/.docs/instructions.md create mode 100644 exercises/practice/roman-numerals/.docs/introduction.md create mode 100644 exercises/practice/roman-numerals/.meta/config.json create mode 100644 exercises/practice/roman-numerals/.meta/example.lfe create mode 100644 exercises/practice/roman-numerals/.meta/tests.toml create mode 100644 exercises/practice/roman-numerals/Makefile create mode 100644 exercises/practice/roman-numerals/rebar.config create mode 100644 exercises/practice/roman-numerals/rebar.lock create mode 100644 exercises/practice/roman-numerals/src/roman-numerals.app.src create mode 100644 exercises/practice/roman-numerals/src/roman-numerals.lfe create mode 100644 exercises/practice/roman-numerals/test/roman-numerals-tests.lfe diff --git a/config.json b/config.json index 3c36054a..6e4cab3a 100644 --- a/config.json +++ b/config.json @@ -351,6 +351,14 @@ "practices": [], "prerequisites": [], "difficulty": 1 + }, + { + "slug": "roman-numerals", + "name": "Roman Numerals", + "uuid": "461223ea-e86b-4b94-9e7a-ac3ec1aa8a12", + "practices": [], + "prerequisites": [], + "difficulty": 1 } ] }, diff --git a/exercises/practice/roman-numerals/.docs/instructions.md b/exercises/practice/roman-numerals/.docs/instructions.md new file mode 100644 index 00000000..50e2f5bf --- /dev/null +++ b/exercises/practice/roman-numerals/.docs/instructions.md @@ -0,0 +1,12 @@ +# Introduction + +Your task is to convert a number from Arabic numerals to Roman numerals. + +For this exercise, we are only concerned about traditional Roman numerals, in which the largest number is MMMCMXCIX (or 3,999). + +~~~~exercism/note +There are lots of different ways to convert between Arabic and Roman numerals. +We recommend taking a naive approach first to familiarise yourself with the concept of Roman numerals and then search for more efficient methods. + +Make sure to check out our Deep Dive video at the end to explore the different approaches you can take! +~~~~ diff --git a/exercises/practice/roman-numerals/.docs/introduction.md b/exercises/practice/roman-numerals/.docs/introduction.md new file mode 100644 index 00000000..6fd942fe --- /dev/null +++ b/exercises/practice/roman-numerals/.docs/introduction.md @@ -0,0 +1,59 @@ +# Description + +Today, most people in the world use Arabic numerals (0–9). +But if you travelled back two thousand years, you'd find that most Europeans were using Roman numerals instead. + +To write a Roman numeral we use the following Latin letters, each of which has a value: + +| M | D | C | L | X | V | I | +| ---- | --- | --- | --- | --- | --- | --- | +| 1000 | 500 | 100 | 50 | 10 | 5 | 1 | + +A Roman numeral is a sequence of these letters, and its value is the sum of the letters' values. +For example, `XVIII` has the value 18 (`10 + 5 + 1 + 1 + 1 = 18`). + +There's one rule that makes things trickier though, and that's that **the same letter cannot be used more than three times in succession**. +That means that we can't express numbers such as 4 with the seemingly natural `IIII`. +Instead, for those numbers, we use a subtraction method between two letters. +So we think of `4` not as `1 + 1 + 1 + 1` but instead as `5 - 1`. +And slightly confusingly to our modern thinking, we write the smaller number first. +This applies only in the following cases: 4 (`IV`), 9 (`IX`), 40 (`XL`), 90 (`XC`), 400 (`CD`) and 900 (`CM`). + +Order matters in Roman numerals! +Letters (and the special compounds above) must be ordered by decreasing value from left to right. + +Here are some examples: + +```text + 105 => CV +---- => -- + 100 => C ++ 5 => V +``` + +```text + 106 => CVI +---- => -- + 100 => C ++ 5 => V ++ 1 => I +``` + +```text + 104 => CIV +---- => --- + 100 => C ++ 4 => IV +``` + +And a final more complex example: + +```text + 1996 => MCMXCVI +----- => ------- + 1000 => M ++ 900 => CM ++ 90 => XC ++ 5 => V ++ 1 => I +``` diff --git a/exercises/practice/roman-numerals/.meta/config.json b/exercises/practice/roman-numerals/.meta/config.json new file mode 100644 index 00000000..5e7d0c3c --- /dev/null +++ b/exercises/practice/roman-numerals/.meta/config.json @@ -0,0 +1,22 @@ +{ + "authors": [ + "kahgoh" + ], + "files": { + "solution": [ + "src/roman-numerals.lfe" + ], + "test": [ + "test/roman-numerals-tests.lfe" + ], + "example": [ + ".meta/example.lfe" + ], + "invalidator": [ + "rebar.config" + ] + }, + "blurb": "Convert modern Arabic numbers into Roman numerals.", + "source": "The Roman Numeral Kata", + "source_url": "https://codingdojo.org/kata/RomanNumerals/" +} diff --git a/exercises/practice/roman-numerals/.meta/example.lfe b/exercises/practice/roman-numerals/.meta/example.lfe new file mode 100644 index 00000000..41cf11e2 --- /dev/null +++ b/exercises/practice/roman-numerals/.meta/example.lfe @@ -0,0 +1,29 @@ +(defmodule roman-numerals + (export (roman 1)) +) + +(defun numerals () '(("I" "V" "X") + ("X" "L" "C") + ("C" "D" "M") + ("M" "" "")) +) + +(defun roman (value) (conv value "" (numerals))) + +(defun conv + ((_remaining acc []) acc) + ((remaining acc (cons (list ones fives tens) next)) (conv (div remaining 10) (++ (roman-digit (rem remaining 10) ones fives tens) acc) next)) +) + +(defun roman-digit + ((0 _ones _fives _tens) "") + ((1 ones _fives tens) ones) + ((2 ones _fives tens) (++ ones ones)) + ((3 ones _fives _tens) (++ ones ones ones)) + ((4 ones fives _tens) (++ ones fives)) + ((5 _ones fives _tens) fives) + ((6 ones fives _tens) (++ fives ones)) + ((7 ones fives _tens) (++ fives ones ones)) + ((8 ones fives _tens) (++ fives ones ones ones)) + ((9 ones _fives tens) (++ ones tens)) +) diff --git a/exercises/practice/roman-numerals/.meta/tests.toml b/exercises/practice/roman-numerals/.meta/tests.toml new file mode 100644 index 00000000..57c6c4be --- /dev/null +++ b/exercises/practice/roman-numerals/.meta/tests.toml @@ -0,0 +1,88 @@ +# 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. + +[19828a3a-fbf7-4661-8ddd-cbaeee0e2178] +description = "1 is I" + +[f088f064-2d35-4476-9a41-f576da3f7b03] +description = "2 is II" + +[b374a79c-3bea-43e6-8db8-1286f79c7106] +description = "3 is III" + +[05a0a1d4-a140-4db1-82e8-fcc21fdb49bb] +description = "4 is IV" + +[57c0f9ad-5024-46ab-975d-de18c430b290] +description = "5 is V" + +[20a2b47f-e57f-4797-a541-0b3825d7f249] +description = "6 is VI" + +[ff3fb08c-4917-4aab-9f4e-d663491d083d] +description = "9 is IX" + +[6d1d82d5-bf3e-48af-9139-87d7165ed509] +description = "16 is XVI" + +[2bda64ca-7d28-4c56-b08d-16ce65716cf6] +description = "27 is XXVII" + +[a1f812ef-84da-4e02-b4f0-89c907d0962c] +description = "48 is XLVIII" + +[607ead62-23d6-4c11-a396-ef821e2e5f75] +description = "49 is XLIX" + +[d5b283d4-455d-4e68-aacf-add6c4b51915] +description = "59 is LIX" + +[4465ffd5-34dc-44f3-ada5-56f5007b6dad] +description = "66 is LXVI" + +[46b46e5b-24da-4180-bfe2-2ef30b39d0d0] +description = "93 is XCIII" + +[30494be1-9afb-4f84-9d71-db9df18b55e3] +description = "141 is CXLI" + +[267f0207-3c55-459a-b81d-67cec7a46ed9] +description = "163 is CLXIII" + +[902ad132-0b4d-40e3-8597-ba5ed611dd8d] +description = "166 is CLXVI" + +[cdb06885-4485-4d71-8bfb-c9d0f496b404] +description = "402 is CDII" + +[6b71841d-13b2-46b4-ba97-dec28133ea80] +description = "575 is DLXXV" + +[dacb84b9-ea1c-4a61-acbb-ce6b36674906] +description = "666 is DCLXVI" + +[432de891-7fd6-4748-a7f6-156082eeca2f] +description = "911 is CMXI" + +[e6de6d24-f668-41c0-88d7-889c0254d173] +description = "1024 is MXXIV" + +[efbe1d6a-9f98-4eb5-82bc-72753e3ac328] +description = "1666 is MDCLXVI" + +[bb550038-d4eb-4be2-a9ce-f21961ac3bc6] +description = "3000 is MMM" + +[3bc4b41c-c2e6-49d9-9142-420691504336] +description = "3001 is MMMI" + +[4e18e96b-5fbb-43df-a91b-9cb511fe0856] +description = "3999 is MMMCMXCIX" diff --git a/exercises/practice/roman-numerals/Makefile b/exercises/practice/roman-numerals/Makefile new file mode 100644 index 00000000..fbb5a7de --- /dev/null +++ b/exercises/practice/roman-numerals/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/roman-numerals/rebar.config b/exercises/practice/roman-numerals/rebar.config new file mode 100644 index 00000000..d53487ac --- /dev/null +++ b/exercises/practice/roman-numerals/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/roman-numerals/rebar.lock b/exercises/practice/roman-numerals/rebar.lock new file mode 100644 index 00000000..d5a6b3b9 --- /dev/null +++ b/exercises/practice/roman-numerals/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/roman-numerals/src/roman-numerals.app.src b/exercises/practice/roman-numerals/src/roman-numerals.app.src new file mode 100644 index 00000000..24cfa578 --- /dev/null +++ b/exercises/practice/roman-numerals/src/roman-numerals.app.src @@ -0,0 +1,11 @@ +%% -*- erlang -*- +{application, 'roman-numerals', + [{description, "Write a function to convert from normal numbers to Roman Numerals."}, + {vsn, "0.0.1"}, + {modules, + ['roman-numerals']}, + {registered, []}, + {applications, + [kernel, stdlib]}, + {included_applications, []}, + {env, []}]}. diff --git a/exercises/practice/roman-numerals/src/roman-numerals.lfe b/exercises/practice/roman-numerals/src/roman-numerals.lfe new file mode 100644 index 00000000..338c1508 --- /dev/null +++ b/exercises/practice/roman-numerals/src/roman-numerals.lfe @@ -0,0 +1,5 @@ +(defmodule roman-numerals + (export (roman 1)) +) + + ; Please implement the roman function \ No newline at end of file diff --git a/exercises/practice/roman-numerals/test/roman-numerals-tests.lfe b/exercises/practice/roman-numerals/test/roman-numerals-tests.lfe new file mode 100644 index 00000000..9672ad71 --- /dev/null +++ b/exercises/practice/roman-numerals/test/roman-numerals-tests.lfe @@ -0,0 +1,83 @@ +(defmodule roman-numerals-tests + (behaviour ltest-unit) + (export all)) + +(include-lib "ltest/include/ltest-macros.lfe") + +(deftest 1-is-I + (is-equal "I" (roman-numerals:roman 1))) + +(deftest 2-is-II + (is-equal "II" (roman-numerals:roman 2))) + +(deftest 3-is-III + (is-equal "III" (roman-numerals:roman 3))) + +(deftest 4-is-IV + (is-equal "IV" (roman-numerals:roman 4))) + +(deftest 5-is-V + (is-equal "V" (roman-numerals:roman 5))) + +(deftest 6-is-VI + (is-equal "VI" (roman-numerals:roman 6))) + +(deftest 9-is-IX + (is-equal "IX" (roman-numerals:roman 9))) + +(deftest 16-is-XVI + (is-equal "XVI" (roman-numerals:roman 16))) + +(deftest 27-is-XXVII + (is-equal "XXVII" (roman-numerals:roman 27))) + +(deftest 48-is-XLVIII + (is-equal "XLVIII" (roman-numerals:roman 48))) + +(deftest 49-is-XLIX + (is-equal "XLIX" (roman-numerals:roman 49))) + +(deftest 59-is-LIX + (is-equal "LIX" (roman-numerals:roman 59))) + +(deftest 66-is-LXVI + (is-equal "LXVI" (roman-numerals:roman 66))) + +(deftest 93-is-XCIII + (is-equal "XCIII" (roman-numerals:roman 93))) + +(deftest 141-is-CXLI + (is-equal "CXLI" (roman-numerals:roman 141))) + +(deftest 163-is-CLXIII + (is-equal "CLXIII" (roman-numerals:roman 163))) + +(deftest 166-is-CLXVI + (is-equal "CLXVI" (roman-numerals:roman 166))) + +(deftest 402-is-CDII + (is-equal "CDII" (roman-numerals:roman 402))) + +(deftest 575-is-DLXXV + (is-equal "DLXXV" (roman-numerals:roman 575))) + +(deftest 666-is-DCLXVI + (is-equal "DCLXVI" (roman-numerals:roman 666))) + +(deftest 911-is-CMXI + (is-equal "CMXI" (roman-numerals:roman 911))) + +(deftest 1024-is-MXXIV + (is-equal "MXXIV" (roman-numerals:roman 1024))) + +(deftest 1666-is-MDCLXVI + (is-equal "MDCLXVI" (roman-numerals:roman 1666))) + +(deftest 3000-is-MMM + (is-equal "MMM" (roman-numerals:roman 3000))) + +(deftest 3001-is-MMMI + (is-equal "MMMI" (roman-numerals:roman 3001))) + +(deftest 3999-is-MMMCMXCIX + (is-equal "MMMCMXCIX" (roman-numerals:roman 3999)))