From 219866b77a8085b006495a5e49bdb3d5c3354d3d Mon Sep 17 00:00:00 2001 From: lucaferranti Date: Fri, 1 Mar 2024 01:19:00 +0200 Subject: [PATCH 1/3] add roman numrals exercise --- config.json | 89 +++++++++------ .../roman-numerals/.docs/instructions.md | 12 ++ .../roman-numerals/.docs/introduction.md | 59 ++++++++++ .../practice/roman-numerals/.meta/config.json | 17 +++ .../roman-numerals/.meta/reference.chpl | 23 ++++ .../practice/roman-numerals/.meta/tests.toml | 88 ++++++++++++++ exercises/practice/roman-numerals/Mason.toml | 8 ++ .../roman-numerals/src/roman-numerals.chpl | 5 + .../practice/roman-numerals/test/tests.chpl | 108 ++++++++++++++++++ 9 files changed, 371 insertions(+), 38 deletions(-) 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/reference.chpl create mode 100644 exercises/practice/roman-numerals/.meta/tests.toml create mode 100644 exercises/practice/roman-numerals/Mason.toml create mode 100644 exercises/practice/roman-numerals/src/roman-numerals.chpl create mode 100644 exercises/practice/roman-numerals/test/tests.chpl diff --git a/config.json b/config.json index c244f9d..d14464f 100644 --- a/config.json +++ b/config.json @@ -16,13 +16,20 @@ "highlightjs_language": "chapel" }, "files": { - "solution": ["src/%{kebab_slug}.chpl"], - "test": ["test/tests.chpl"], - "example": [".meta/reference.chpl"], - "exemplar": [".meta/reference.chpl"] + "solution": [ + "src/%{kebab_slug}.chpl" + ], + "test": [ + "test/tests.chpl" + ], + "example": [ + ".meta/reference.chpl" + ], + "exemplar": [ + ".meta/reference.chpl" + ] }, "exercises": { - "concept": [], "practice": [ { "slug": "hello-world", @@ -41,161 +48,167 @@ "difficulty": 1 }, { - "uuid": "0b8f9871-7dd8-4f51-b8e3-be60c9297643", "slug": "resistor-color", "name": "Resistor Color", + "uuid": "0b8f9871-7dd8-4f51-b8e3-be60c9297643", "practices": [], "prerequisites": [], "difficulty": 1 }, { - "uuid": "fec30071-3f47-462d-be6d-5f14d6ad0d4a", "slug": "leap", "name": "Leap", + "uuid": "fec30071-3f47-462d-be6d-5f14d6ad0d4a", "practices": [], "prerequisites": [], "difficulty": 1 }, { - "uuid": "b3ea1ce9-db60-4e24-9f74-b9c58e7ede4b", "slug": "darts", "name": "Darts", + "uuid": "b3ea1ce9-db60-4e24-9f74-b9c58e7ede4b", "practices": [], "prerequisites": [], "difficulty": 1 }, { - "uuid": "93e093ca-6c17-472c-8acc-75eec4d6e052", "slug": "gigasecond", "name": "Gigasecond", + "uuid": "93e093ca-6c17-472c-8acc-75eec4d6e052", "practices": [], "prerequisites": [], "difficulty": 1 }, { - "uuid": "2ad98a06-67c9-4efd-8f8f-b687ef72f754", "slug": "collatz-conjecture", "name": "Collatz Conjecture", + "uuid": "2ad98a06-67c9-4efd-8f8f-b687ef72f754", "practices": [], "prerequisites": [], "difficulty": 1 }, { - "uuid": "69b04b2c-164c-491d-9731-506c0983325e", "slug": "isogram", "name": "Isogram", + "uuid": "69b04b2c-164c-491d-9731-506c0983325e", "practices": [], "prerequisites": [], "difficulty": 2 }, { - "uuid": "d4705802-7ea5-459b-86d1-e0b171e6d0f5", "slug": "high-scores", "name": "High Scores", + "uuid": "d4705802-7ea5-459b-86d1-e0b171e6d0f5", "practices": [], "prerequisites": [], "difficulty": 1 }, { - "uuid": "6b3c464c-bb34-4667-8dd6-dc9d0c06326c", "slug": "nucleotide-count", "name": "Nucleotide Count", + "uuid": "6b3c464c-bb34-4667-8dd6-dc9d0c06326c", "practices": [], "prerequisites": [], "difficulty": 3 }, { - "uuid": "b0317cb6-adaf-4fa7-a050-ad40e1139413", "slug": "raindrops", "name": "Raindrops", + "uuid": "b0317cb6-adaf-4fa7-a050-ad40e1139413", "practices": [], "prerequisites": [], "difficulty": 4 }, { - "uuid": "bad2bd29-19b8-44ce-a1f4-1694b0c468a2", "slug": "bob", "name": "Bob", + "uuid": "bad2bd29-19b8-44ce-a1f4-1694b0c468a2", "practices": [], "prerequisites": [], "difficulty": 4 }, { - "uuid": "f36a10f8-560c-4427-869c-a3bb5f260a81", "slug": "allergies", "name": "Allergies", + "uuid": "f36a10f8-560c-4427-869c-a3bb5f260a81", "practices": [], "prerequisites": [], "difficulty": 5 }, { - "uuid": "23034fea-e43c-4ce7-9ef9-289793279feb", "slug": "series", "name": "Series", + "uuid": "23034fea-e43c-4ce7-9ef9-289793279feb", "practices": [], "prerequisites": [], "difficulty": 4 }, { - "uuid": "3686b062-b9d2-4ece-ab7b-9b2700cd4403", - "slug": "circular-buffer", - "name": "Circular Buffer", - "practices": [], - "prerequisites": [], - "difficulty": 7 + "slug": "circular-buffer", + "name": "Circular Buffer", + "uuid": "3686b062-b9d2-4ece-ab7b-9b2700cd4403", + "practices": [], + "prerequisites": [], + "difficulty": 7 }, { - "uuid": "5046b55e-5bdc-4f58-8551-08158f5a512e", "slug": "diamond", "name": "Diamond", + "uuid": "5046b55e-5bdc-4f58-8551-08158f5a512e", "practices": [], "prerequisites": [], "difficulty": 8 }, { - "uuid": "e2d1d506-2f74-4890-99d0-e8e39c3f3259", "slug": "yacht", "name": "Yacht", + "uuid": "e2d1d506-2f74-4890-99d0-e8e39c3f3259", "practices": [], "prerequisites": [], "difficulty": 7 }, { - "uuid": "a42d5d17-fd37-4f11-9220-00c2aab696be", "slug": "meetup", "name": "Meetup", + "uuid": "a42d5d17-fd37-4f11-9220-00c2aab696be", "practices": [], "prerequisites": [], "difficulty": 5 }, { - "uuid": "29134363-4e01-4ccf-b26f-6f0a5dd80eef", "slug": "dominoes", "name": "Dominoes", + "uuid": "29134363-4e01-4ccf-b26f-6f0a5dd80eef", "practices": [], "prerequisites": [], "difficulty": 10 }, { - "uuid": "f720055b-312a-44af-b05b-4beaf484ad2e", - "slug": "difference-of-squares", - "name": "Difference of Squares", - "practices": [], - "prerequisites": [], - "difficulty": 1 + "slug": "difference-of-squares", + "name": "Difference of Squares", + "uuid": "f720055b-312a-44af-b05b-4beaf484ad2e", + "practices": [], + "prerequisites": [], + "difficulty": 1 + }, + { + "slug": "roman-numerals", + "name": "Roman Numerals", + "uuid": "bc0a8910-6d7e-4ee8-8a5c-531e92ce7c4e", + "practices": [], + "prerequisites": [], + "difficulty": 2 } ] }, - "concepts": [], - "key_features": [], "tags": [ - "typing/static", "execution_mode/compiled", - "platform/mac", - "platform/linux", "paradigm/imperative", "paradigm/object_oriented", + "platform/linux", + "platform/mac", "runtime/standalone_executable", + "typing/static", "used_for/scientific_calculations" ] } diff --git a/exercises/practice/roman-numerals/.docs/instructions.md b/exercises/practice/roman-numerals/.docs/instructions.md new file mode 100644 index 0000000..50e2f5b --- /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 0000000..6fd942f --- /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 0000000..32fe914 --- /dev/null +++ b/exercises/practice/roman-numerals/.meta/config.json @@ -0,0 +1,17 @@ +{ + "authors": ["lucaferranti"], + "files": { + "solution": [ + "src/roman-numerals.chpl" + ], + "test": [ + "test/tests.chpl" + ], + "example": [ + ".meta/reference.chpl" + ] + }, + "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/reference.chpl b/exercises/practice/roman-numerals/.meta/reference.chpl new file mode 100644 index 0000000..6af14fb --- /dev/null +++ b/exercises/practice/roman-numerals/.meta/reference.chpl @@ -0,0 +1,23 @@ +module RomanNumerals { + + + private proc helper(n: int, symbols: 3 * string) { + var (unit, five, next) = symbols; + if n == 9 then return unit + next; + else if n >= 5 then return five + (unit * (n - 5)); + else if n == 4 then return unit + five; + else return unit * n; + } + + proc toRomanNumeral(in n: int): string { + var res : string = "M" * (n / 1000); + n = n % 1000; + res += helper(n / 100, ("C", "D", "M")); + n = n % 100; + res += helper(n / 10, ("X", "L", "C")); + n = n % 10; + res += helper(n, ("I", "V", "X")); + return res; + } + +} diff --git a/exercises/practice/roman-numerals/.meta/tests.toml b/exercises/practice/roman-numerals/.meta/tests.toml new file mode 100644 index 0000000..57c6c4b --- /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/Mason.toml b/exercises/practice/roman-numerals/Mason.toml new file mode 100644 index 0000000..9830481 --- /dev/null +++ b/exercises/practice/roman-numerals/Mason.toml @@ -0,0 +1,8 @@ +[brick] +name="roman-numerals" +version="0.1.0" +chplVersion="1.33.0" +type="application" + +[dependencies] + diff --git a/exercises/practice/roman-numerals/src/roman-numerals.chpl b/exercises/practice/roman-numerals/src/roman-numerals.chpl new file mode 100644 index 0000000..d143d2d --- /dev/null +++ b/exercises/practice/roman-numerals/src/roman-numerals.chpl @@ -0,0 +1,5 @@ +module RomanNumerals { + +// write your solution here + +} diff --git a/exercises/practice/roman-numerals/test/tests.chpl b/exercises/practice/roman-numerals/test/tests.chpl new file mode 100644 index 0000000..df3fcd7 --- /dev/null +++ b/exercises/practice/roman-numerals/test/tests.chpl @@ -0,0 +1,108 @@ +use UnitTest; +use RomanNumerals; + +proc test_1_is_I(test : borrowed Test) throws { + test.assertEqual(toRomanNumeral(1), "I"); +} + +proc test_2_is_II(test : borrowed Test) throws { + test.assertEqual(toRomanNumeral(2), "II"); +} + +proc test_3_is_III(test : borrowed Test) throws { + test.assertEqual(toRomanNumeral(3), "III"); +} + +proc test_4_is_IV(test : borrowed Test) throws { + test.assertEqual(toRomanNumeral(4), "IV"); +} + +proc test_5_is_V(test : borrowed Test) throws { + test.assertEqual(toRomanNumeral(5), "V"); +} + +proc test_6_is_VI(test : borrowed Test) throws { + test.assertEqual(toRomanNumeral(6), "VI"); +} + +proc test_9_is_IX(test : borrowed Test) throws { + test.assertEqual(toRomanNumeral(9), "IX"); +} + +proc test_16_is_XVI(test : borrowed Test) throws { + test.assertEqual(toRomanNumeral(16), "XVI"); +} + +proc test_27_is_XXVII(test : borrowed Test) throws { + test.assertEqual(toRomanNumeral(27), "XXVII"); +} + +proc test_48_is_XLVIII(test : borrowed Test) throws { + test.assertEqual(toRomanNumeral(48), "XLVIII"); +} + +proc test_49_is_XLIX(test : borrowed Test) throws { + test.assertEqual(toRomanNumeral(49), "XLIX"); +} + +proc test_59_is_LIX(test : borrowed Test) throws { + test.assertEqual(toRomanNumeral(59), "LIX"); +} + +proc test_66_is_LXVI(test : borrowed Test) throws { + test.assertEqual(toRomanNumeral(66), "LXVI"); +} + +proc test_93_is_XCIII(test : borrowed Test) throws { + test.assertEqual(toRomanNumeral(93), "XCIII"); +} + +proc test_141_is_CXLI(test : borrowed Test) throws { + test.assertEqual(toRomanNumeral(141), "CXLI"); +} + +proc test_163_is_CLXIII(test : borrowed Test) throws { + test.assertEqual(toRomanNumeral(163), "CLXIII"); +} + +proc test_166_is_CLXVI(test : borrowed Test) throws { + test.assertEqual(toRomanNumeral(166), "CLXVI"); +} + +proc test_402_is_CDII(test : borrowed Test) throws { + test.assertEqual(toRomanNumeral(402), "CDII"); +} + +proc test_575_is_DLXXV(test : borrowed Test) throws { + test.assertEqual(toRomanNumeral(575), "DLXXV"); +} + +proc test_666_is_DCLXVI(test : borrowed Test) throws { + test.assertEqual(toRomanNumeral(666), "DCLXVI"); +} + +proc test_911_is_CMXI(test : borrowed Test) throws { + test.assertEqual(toRomanNumeral(911), "CMXI"); +} + +proc test_1024_is_MXXIV(test : borrowed Test) throws { + test.assertEqual(toRomanNumeral(1024), "MXXIV"); +} + +proc test_1666_is_MDCLXVI(test : borrowed Test) throws { + test.assertEqual(toRomanNumeral(1666), "MDCLXVI"); +} + +proc test_3000_is_MMM(test : borrowed Test) throws { + test.assertEqual(toRomanNumeral(3000), "MMM"); +} + +proc test_3001_is_MMMI(test : borrowed Test) throws { + test.assertEqual(toRomanNumeral(3001), "MMMI"); +} + +proc test_3999_is_MMMCMXCIX(test : borrowed Test) throws { + test.assertEqual(toRomanNumeral(3999), "MMMCMXCIX"); +} + +UnitTest.main(); From b6127c87a727d541c9539f925cf2619f2bb536a6 Mon Sep 17 00:00:00 2001 From: lucaferranti Date: Fri, 1 Mar 2024 01:21:14 +0200 Subject: [PATCH 2/3] tweak --- exercises/practice/roman-numerals/.meta/reference.chpl | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/exercises/practice/roman-numerals/.meta/reference.chpl b/exercises/practice/roman-numerals/.meta/reference.chpl index 6af14fb..c6908e4 100644 --- a/exercises/practice/roman-numerals/.meta/reference.chpl +++ b/exercises/practice/roman-numerals/.meta/reference.chpl @@ -11,11 +11,14 @@ module RomanNumerals { proc toRomanNumeral(in n: int): string { var res : string = "M" * (n / 1000); - n = n % 1000; + n %= 1000; + res += helper(n / 100, ("C", "D", "M")); - n = n % 100; + n %= 100; + res += helper(n / 10, ("X", "L", "C")); - n = n % 10; + n %= 10; + res += helper(n, ("I", "V", "X")); return res; } From 3ba0974812783afcfc0fff1802f7a70f7388db64 Mon Sep 17 00:00:00 2001 From: lucaferranti Date: Fri, 1 Mar 2024 01:25:44 +0200 Subject: [PATCH 3/3] last tweak --- exercises/practice/roman-numerals/.meta/reference.chpl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/exercises/practice/roman-numerals/.meta/reference.chpl b/exercises/practice/roman-numerals/.meta/reference.chpl index c6908e4..bfa101e 100644 --- a/exercises/practice/roman-numerals/.meta/reference.chpl +++ b/exercises/practice/roman-numerals/.meta/reference.chpl @@ -1,8 +1,7 @@ module RomanNumerals { - private proc helper(n: int, symbols: 3 * string) { - var (unit, five, next) = symbols; + private proc helper(n : int, (unit, five, next) : 3 * string) { if n == 9 then return unit + next; else if n >= 5 then return five + (unit * (n - 5)); else if n == 4 then return unit + five;