From bc7cf94022b1293421977976611800aebf3bba7f Mon Sep 17 00:00:00 2001 From: Eric Willigers Date: Thu, 18 Jan 2024 17:24:16 +1100 Subject: [PATCH 1/2] feat: add roman-numerals exercise --- config.json | 8 ++ .../roman-numerals/.docs/instructions.md | 41 +++++++ .../practice/roman-numerals/.meta/config.json | 19 ++++ .../practice/roman-numerals/.meta/example.v | 30 +++++ .../practice/roman-numerals/.meta/tests.toml | 81 ++++++++++++++ .../practice/roman-numerals/roman-numerals.v | 4 + exercises/practice/roman-numerals/run_test.v | 105 ++++++++++++++++++ 7 files changed, 288 insertions(+) create mode 100644 exercises/practice/roman-numerals/.docs/instructions.md create mode 100644 exercises/practice/roman-numerals/.meta/config.json create mode 100644 exercises/practice/roman-numerals/.meta/example.v create mode 100644 exercises/practice/roman-numerals/.meta/tests.toml create mode 100644 exercises/practice/roman-numerals/roman-numerals.v create mode 100644 exercises/practice/roman-numerals/run_test.v diff --git a/config.json b/config.json index 15743ab..cf96576 100644 --- a/config.json +++ b/config.json @@ -577,6 +577,14 @@ "prerequisites": [], "difficulty": 3 }, + { + "slug": "roman-numerals", + "name": "Roman Numerals", + "uuid": "eeab8f00-e9ac-4696-ba1d-d1c418199d5d", + "practices": [], + "prerequisites": [], + "difficulty": 3 + }, { "slug": "etl", "name": "ETL", diff --git a/exercises/practice/roman-numerals/.docs/instructions.md b/exercises/practice/roman-numerals/.docs/instructions.md new file mode 100644 index 0000000..247ea08 --- /dev/null +++ b/exercises/practice/roman-numerals/.docs/instructions.md @@ -0,0 +1,41 @@ +# Instructions + +Write a function to convert from normal numbers to Roman Numerals. + +The Romans were a clever bunch. +They conquered most of Europe and ruled it for hundreds of years. +They invented concrete and straight roads and even bikinis. +One thing they never discovered though was the number zero. +This made writing and dating extensive histories of their exploits slightly more challenging, but the system of numbers they came up with is still in use today. +For example the BBC uses Roman numerals to date their programs. + +The Romans wrote numbers using letters - I, V, X, L, C, D, M. +(notice these letters have lots of straight lines and are hence easy to hack into stone tablets). + +```text + 1 => I +10 => X + 7 => VII +``` + +The maximum number supported by this notation is 3,999. +(The Romans themselves didn't tend to go any higher) + +Wikipedia says: Modern Roman numerals ... are written by expressing each digit separately starting with the left most digit and skipping any digit with a value of zero. + +To see this in practice, consider the example of 1990. + +In Roman numerals 1990 is MCMXC: + +1000=M +900=CM +90=XC + +2008 is written as MMVIII: + +2000=MM +8=VIII + +Learn more about [Roman numerals on Wikipedia][roman-numerals]. + +[roman-numerals]: https://wiki.imperivm-romanvm.com/wiki/Roman_Numerals diff --git a/exercises/practice/roman-numerals/.meta/config.json b/exercises/practice/roman-numerals/.meta/config.json new file mode 100644 index 0000000..5c68c53 --- /dev/null +++ b/exercises/practice/roman-numerals/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "keiravillekode" + ], + "files": { + "solution": [ + "roman-numerals.v" + ], + "test": [ + "run_test.v" + ], + "example": [ + ".meta/example.v" + ] + }, + "blurb": "Write a function to convert from normal numbers to Roman Numerals.", + "source": "The Roman Numeral Kata", + "source_url": "https://codingdojo.org/kata/RomanNumerals/" +} diff --git a/exercises/practice/roman-numerals/.meta/example.v b/exercises/practice/roman-numerals/.meta/example.v new file mode 100644 index 0000000..8cbe7e5 --- /dev/null +++ b/exercises/practice/roman-numerals/.meta/example.v @@ -0,0 +1,30 @@ +module main + +const ones = [u8(`I`), `X`, `C`, `M`] + +const fives = [u8(`V`), `L`, `D`] + +fn write(mut buffer []u8, digit int, place int) { + if digit % 5 == 4 { + buffer << ones[place] + write(mut buffer, digit + 1, place) + } else if digit == 10 { + write(mut buffer, 1, place + 1) + } else if digit >= 5 { + buffer << fives[place] + write(mut buffer, digit - 5, place) + } else if digit > 0 { + buffer << ones[place] + write(mut buffer, digit - 1, place) + } +} + +fn roman(number int) string { + assert number < 4000 + mut buffer := []u8{} + write(mut buffer, number / 1000, 3) + write(mut buffer, number / 100 % 10, 2) + write(mut buffer, number / 10 % 10, 1) + write(mut buffer, number % 10, 0) + return buffer.bytestr() +} diff --git a/exercises/practice/roman-numerals/.meta/tests.toml b/exercises/practice/roman-numerals/.meta/tests.toml new file mode 100644 index 0000000..c92ccc5 --- /dev/null +++ b/exercises/practice/roman-numerals/.meta/tests.toml @@ -0,0 +1,81 @@ +# This is an auto-generated file. Regular comments will be removed when this +# file is regenerated. Regenerating will not touch any manually added keys, +# so comments can be added in 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/roman-numerals.v b/exercises/practice/roman-numerals/roman-numerals.v new file mode 100644 index 0000000..0878fa2 --- /dev/null +++ b/exercises/practice/roman-numerals/roman-numerals.v @@ -0,0 +1,4 @@ +module main + +fn roman(number int) string { +} diff --git a/exercises/practice/roman-numerals/run_test.v b/exercises/practice/roman-numerals/run_test.v new file mode 100644 index 0000000..03d9595 --- /dev/null +++ b/exercises/practice/roman-numerals/run_test.v @@ -0,0 +1,105 @@ +module main + +fn test_1_is_i() { + assert roman(1) == 'I' +} + +fn test_2_is_ii() { + assert roman(2) == 'II' +} + +fn test_3_is_iii() { + assert roman(3) == 'III' +} + +fn test_4_is_iv() { + assert roman(4) == 'IV' +} + +fn test_5_is_v() { + assert roman(5) == 'V' +} + +fn test_6_is_vi() { + assert roman(6) == 'VI' +} + +fn test_9_is_ix() { + assert roman(9) == 'IX' +} + +fn test_16_is_xvi() { + assert roman(16) == 'XVI' +} + +fn test_27_is_xxvii() { + assert roman(27) == 'XXVII' +} + +fn test_48_is_xlviii() { + assert roman(48) == 'XLVIII' +} + +fn test_49_is_xlix() { + assert roman(49) == 'XLIX' +} + +fn test_59_is_lix() { + assert roman(59) == 'LIX' +} + +fn test_66_is_lxvi() { + assert roman(66) == 'LXVI' +} + +fn test_93_is_xciii() { + assert roman(93) == 'XCIII' +} + +fn test_141_is_cxli() { + assert roman(141) == 'CXLI' +} + +fn test_163_is_clxiii() { + assert roman(163) == 'CLXIII' +} + +fn test_166_is_clxvi() { + assert roman(166) == 'CLXVI' +} + +fn test_402_is_cdii() { + assert roman(402) == 'CDII' +} + +fn test_575_is_dlxxv() { + assert roman(575) == 'DLXXV' +} + +fn test_666_is_dclxvi() { + assert roman(666) == 'DCLXVI' +} + +fn test_911_is_cmxi() { + assert roman(911) == 'CMXI' +} + +fn test_1024_is_mxxiv() { + assert roman(1024) == 'MXXIV' +} + +fn test_1666_is_mdclxvi() { + assert roman(1666) == 'MDCLXVI' +} + +fn test_3000_is_mmm() { + assert roman(3000) == 'MMM' +} + +fn test_3001_is_mmmi() { + assert roman(3001) == 'MMMI' +} + +fn test_3999_is_mmmcmxcix() { + assert roman(3999) == 'MMMCMXCIX' +} From cb095d4c5f3fa04ee2a85cf91941020434fc91bb Mon Sep 17 00:00:00 2001 From: Eric Willigers Date: Sun, 28 Jan 2024 08:55:24 +1100 Subject: [PATCH 2/2] sync roman-numerals instructions --- .../roman-numerals/.docs/instructions.md | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/exercises/practice/roman-numerals/.docs/instructions.md b/exercises/practice/roman-numerals/.docs/instructions.md index 247ea08..f3b85a7 100644 --- a/exercises/practice/roman-numerals/.docs/instructions.md +++ b/exercises/practice/roman-numerals/.docs/instructions.md @@ -9,8 +9,7 @@ One thing they never discovered though was the number zero. This made writing and dating extensive histories of their exploits slightly more challenging, but the system of numbers they came up with is still in use today. For example the BBC uses Roman numerals to date their programs. -The Romans wrote numbers using letters - I, V, X, L, C, D, M. -(notice these letters have lots of straight lines and are hence easy to hack into stone tablets). +The Romans wrote numbers using letters - I, V, X, L, C, D, M; notice these letters have lots of straight lines and are hence easy to hack into stone tablets. ```text 1 => I @@ -19,22 +18,29 @@ The Romans wrote numbers using letters - I, V, X, L, C, D, M. ``` The maximum number supported by this notation is 3,999. -(The Romans themselves didn't tend to go any higher) -Wikipedia says: Modern Roman numerals ... are written by expressing each digit separately starting with the left most digit and skipping any digit with a value of zero. +Wikipedia says: Modern Roman numerals [...] are written by expressing each digit separately starting with the left most digit and skipping any digit with a value of zero. To see this in practice, consider the example of 1990. In Roman numerals 1990 is MCMXC: -1000=M -900=CM -90=XC +```text + 1000 => M + 900 => CM ++ 90 => XC +----- => ----- + 1990 => MCMXC +``` 2008 is written as MMVIII: -2000=MM -8=VIII +```text + 2000 => MM ++ 8 => VIII +----- => ------ + 2008 => MMVIII +``` Learn more about [Roman numerals on Wikipedia][roman-numerals].