From 5198ed48117068065fd2b3f7b63916fe20b93cec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nenad=20Misi=C4=87?= Date: Tue, 25 Jun 2024 11:07:59 +0200 Subject: [PATCH] Add Roman numerals exercise (#20) * fix verify exercises to not error when targeting single exercise * Add roman-numerals exercise * add comment for const arrays * remove tool-versions --- bin/verify-exercises | 3 + concepts/type-casting/.meta/config.json | 7 + concepts/type-casting/about.md | 1 + concepts/type-casting/introduction.md | 1 + concepts/type-casting/links.json | 1 + config.json | 18 +- .../roman-numerals/.docs/instructions.md | 12 + .../roman-numerals/.docs/introduction.md | 59 +++++ .../practice/roman-numerals/.meta/config.json | 20 ++ .../roman-numerals/.meta/example.cairo | 56 +++++ .../practice/roman-numerals/.meta/tests.toml | 91 ++++++++ exercises/practice/roman-numerals/Scarb.toml | 4 + .../practice/roman-numerals/src/lib.cairo | 26 +++ .../practice/roman-numerals/src/tests.cairo | 217 ++++++++++++++++++ 14 files changed, 515 insertions(+), 1 deletion(-) create mode 100644 concepts/type-casting/.meta/config.json create mode 100644 concepts/type-casting/about.md create mode 100644 concepts/type-casting/introduction.md create mode 100644 concepts/type-casting/links.json 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.cairo create mode 100644 exercises/practice/roman-numerals/.meta/tests.toml create mode 100644 exercises/practice/roman-numerals/Scarb.toml create mode 100644 exercises/practice/roman-numerals/src/lib.cairo create mode 100644 exercises/practice/roman-numerals/src/tests.cairo diff --git a/bin/verify-exercises b/bin/verify-exercises index 7e6bc2e6..89239d47 100755 --- a/bin/verify-exercises +++ b/bin/verify-exercises @@ -17,6 +17,9 @@ slug="${1:-*}" verify_exercise() { exercises_path="$repo/exercises/$1/$slug" + if ! test -d "$exercises_path"; then + return + fi source_file_name="$2" tmp_file=$(mktemp) trap 'rm $tmp_file' EXIT INT TERM diff --git a/concepts/type-casting/.meta/config.json b/concepts/type-casting/.meta/config.json new file mode 100644 index 00000000..52914445 --- /dev/null +++ b/concepts/type-casting/.meta/config.json @@ -0,0 +1,7 @@ +{ + "blurb": "", + "authors": [ + "misicnenad" + ], + "contributors": [] +} diff --git a/concepts/type-casting/about.md b/concepts/type-casting/about.md new file mode 100644 index 00000000..2732c4ee --- /dev/null +++ b/concepts/type-casting/about.md @@ -0,0 +1 @@ +# Type Casting diff --git a/concepts/type-casting/introduction.md b/concepts/type-casting/introduction.md new file mode 100644 index 00000000..e10b99d0 --- /dev/null +++ b/concepts/type-casting/introduction.md @@ -0,0 +1 @@ +# Introduction diff --git a/concepts/type-casting/links.json b/concepts/type-casting/links.json new file mode 100644 index 00000000..fe51488c --- /dev/null +++ b/concepts/type-casting/links.json @@ -0,0 +1 @@ +[] diff --git a/config.json b/config.json index e80bcaa6..969f35c7 100644 --- a/config.json +++ b/config.json @@ -44,7 +44,6 @@ "traits" ], "prerequisites": [], - "difficulty": 2, "status": "wip" } ], @@ -124,6 +123,18 @@ ], "prerequisites": [], "difficulty": 4 + }, + { + "slug": "roman-numerals", + "name": "Roman Numerals", + "uuid": "3fb4bd2f-bc5f-4a67-b9cc-49a786245876", + "practices": [ + "tuples", + "traits", + "type-casting" + ], + "prerequisites": [], + "difficulty": 2 } ], "foregone": [ @@ -195,6 +206,11 @@ "uuid": "0b069f59-f8b4-4979-935a-c3172538f4c9", "slug": "operator-overload", "name": "Operator Overload" + }, + { + "uuid": "b0ea24b3-3520-42c2-9177-0550247fa2bd", + "slug": "type-casting", + "name": "Type Casting" } ], "key_features": [ 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..c621aa84 --- /dev/null +++ b/exercises/practice/roman-numerals/.meta/config.json @@ -0,0 +1,20 @@ +{ + "authors": [ + "misicnenad" + ], + "files": { + "solution": [ + "src/lib.cairo", + "Scarb.toml" + ], + "test": [ + "src/tests.cairo" + ], + "example": [ + ".meta/example.cairo" + ] + }, + "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.cairo b/exercises/practice/roman-numerals/.meta/example.cairo new file mode 100644 index 00000000..3cfcad08 --- /dev/null +++ b/exercises/practice/roman-numerals/.meta/example.cairo @@ -0,0 +1,56 @@ +#[derive(Drop, Debug, PartialEq)] +pub struct Roman { + value: ByteArray, +} + +#[generate_trait] +impl RomanImpl of RomanTrait { + fn from_u32(num: u32) -> Roman { + // it will soon be possible to use constant array variables + // for now we have to define them within the function + let mut ROMAN_MAP: Array<(u32, ByteArray)> = array![ + (1000, "M"), + (900, "CM"), + (500, "D"), + (400, "CD"), + (100, "C"), + (90, "XC"), + (50, "L"), + (40, "XL"), + (10, "X"), + (9, "IX"), + (5, "V"), + (4, "IV"), + (1, "I"), + ]; + + let mut value: ByteArray = ""; + let mut current_num = num; + while let Option::Some((numeric, roman_string)) = ROMAN_MAP + .pop_front() { + while current_num >= numeric { + value.append(@roman_string); + current_num -= numeric; + } + }; + + Roman { value } + } +} + +impl U32IntoRoman of Into { + #[must_use] + fn into(self: u32) -> Roman { + RomanImpl::from_u32(self) + } +} + +impl RomanIIntoByteArray of Into { + #[must_use] + fn into(self: Roman) -> ByteArray { + self.value + } +} + +#[cfg(test)] +mod tests; diff --git a/exercises/practice/roman-numerals/.meta/tests.toml b/exercises/practice/roman-numerals/.meta/tests.toml new file mode 100644 index 00000000..709011b5 --- /dev/null +++ b/exercises/practice/roman-numerals/.meta/tests.toml @@ -0,0 +1,91 @@ +# 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" + +[2f89cad7-73f6-4d1b-857b-0ef531f68b7e] +description = "3888 is MMMDCCCLXXXVIII" + +[4e18e96b-5fbb-43df-a91b-9cb511fe0856] +description = "3999 is MMMCMXCIX" diff --git a/exercises/practice/roman-numerals/Scarb.toml b/exercises/practice/roman-numerals/Scarb.toml new file mode 100644 index 00000000..763775ca --- /dev/null +++ b/exercises/practice/roman-numerals/Scarb.toml @@ -0,0 +1,4 @@ +[package] +name = "roman_numerals" +version = "0.1.0" +edition = "2023_11" diff --git a/exercises/practice/roman-numerals/src/lib.cairo b/exercises/practice/roman-numerals/src/lib.cairo new file mode 100644 index 00000000..1433e68b --- /dev/null +++ b/exercises/practice/roman-numerals/src/lib.cairo @@ -0,0 +1,26 @@ +#[derive(Drop, Debug, PartialEq)] +pub struct Roman {} + +#[generate_trait] +impl RomanImpl of RomanTrait { + fn new(num: u32) -> Roman { + panic!() + } +} + +impl U32IntoRoman of Into { + #[must_use] + fn into(self: u32) -> Roman { + panic!() + } +} + +impl RomanIIntoByteArray of Into { + #[must_use] + fn into(self: Roman) -> ByteArray { + panic!() + } +} + +#[cfg(test)] +mod tests; diff --git a/exercises/practice/roman-numerals/src/tests.cairo b/exercises/practice/roman-numerals/src/tests.cairo new file mode 100644 index 00000000..843890ae --- /dev/null +++ b/exercises/practice/roman-numerals/src/tests.cairo @@ -0,0 +1,217 @@ +use roman_numerals::Roman; + +#[test] +fn number_1_is_i() { + let input = 1_u32; + let output: Roman = input.into(); + let expected: ByteArray = "I"; + assert_eq!(output.into(), expected); +} + +#[test] +fn number_2_is_ii() { + let input = 2_u32; + let output: Roman = input.into(); + let expected: ByteArray = "II"; + assert_eq!(output.into(), expected); +} + +#[test] +fn number_3_is_iii() { + let input = 3_u32; + let output: Roman = input.into(); + let expected: ByteArray = "III"; + assert_eq!(output.into(), expected); +} + +#[test] +fn number_4_is_iv() { + let input = 4_u32; + let output: Roman = input.into(); + let expected: ByteArray = "IV"; + assert_eq!(output.into(), expected); +} + +#[test] +fn number_5_is_v() { + let input = 5_u32; + let output: Roman = input.into(); + let expected: ByteArray = "V"; + assert_eq!(output.into(), expected); +} + +#[test] +fn number_6_is_vi() { + let input = 6_u32; + let output: Roman = input.into(); + let expected: ByteArray = "VI"; + assert_eq!(output.into(), expected); +} + +#[test] +fn number_9_is_ix() { + let input = 9_u32; + let output: Roman = input.into(); + let expected: ByteArray = "IX"; + assert_eq!(output.into(), expected); +} + +#[test] +fn number_16_is_xvi() { + let input = 16_u32; + let output: Roman = input.into(); + let expected: ByteArray = "XVI"; + assert_eq!(output.into(), expected); +} + +#[test] +fn number_27_is_xxvii() { + let input = 27_u32; + let output: Roman = input.into(); + let expected: ByteArray = "XXVII"; + assert_eq!(output.into(), expected); +} + +#[test] +fn number_48_is_xlviii() { + let input = 48_u32; + let output: Roman = input.into(); + let expected: ByteArray = "XLVIII"; + assert_eq!(output.into(), expected); +} + +#[test] +fn number_49_is_xlix() { + let input = 49_u32; + let output: Roman = input.into(); + let expected: ByteArray = "XLIX"; + assert_eq!(output.into(), expected); +} + +#[test] +fn number_59_is_lix() { + let input = 59_u32; + let output: Roman = input.into(); + let expected: ByteArray = "LIX"; + assert_eq!(output.into(), expected); +} + +#[test] +fn number_66_is_lxvi() { + let input = 66_u32; + let output: Roman = input.into(); + let expected: ByteArray = "LXVI"; + assert_eq!(output.into(), expected); +} + +#[test] +fn number_93_is_xciii() { + let input = 93_u32; + let output: Roman = input.into(); + let expected: ByteArray = "XCIII"; + assert_eq!(output.into(), expected); +} + +#[test] +fn number_141_is_cxli() { + let input = 141_u32; + let output: Roman = input.into(); + let expected: ByteArray = "CXLI"; + assert_eq!(output.into(), expected); +} + +#[test] +fn number_163_is_clxiii() { + let input = 163_u32; + let output: Roman = input.into(); + let expected: ByteArray = "CLXIII"; + assert_eq!(output.into(), expected); +} + +#[test] +fn number_166_is_clxvi() { + let input = 166_u32; + let output: Roman = input.into(); + let expected: ByteArray = "CLXVI"; + assert_eq!(output.into(), expected); +} + +#[test] +fn number_402_is_cdii() { + let input = 402_u32; + let output: Roman = input.into(); + let expected: ByteArray = "CDII"; + assert_eq!(output.into(), expected); +} + +#[test] +fn number_575_is_dlxxv() { + let input = 575_u32; + let output: Roman = input.into(); + let expected: ByteArray = "DLXXV"; + assert_eq!(output.into(), expected); +} + +#[test] +fn number_666_is_dclxvi() { + let input = 666_u32; + let output: Roman = input.into(); + let expected: ByteArray = "DCLXVI"; + assert_eq!(output.into(), expected); +} + +#[test] +fn number_911_is_cmxi() { + let input = 911_u32; + let output: Roman = input.into(); + let expected: ByteArray = "CMXI"; + assert_eq!(output.into(), expected); +} + +#[test] +fn number_1024_is_mxxiv() { + let input = 1024_u32; + let output: Roman = input.into(); + let expected: ByteArray = "MXXIV"; + assert_eq!(output.into(), expected); +} + +#[test] +fn number_1666_is_mdclxvi() { + let input = 1666_u32; + let output: Roman = input.into(); + let expected: ByteArray = "MDCLXVI"; + assert_eq!(output.into(), expected); +} + +#[test] +fn number_3000_is_mmm() { + let input = 3000_u32; + let output: Roman = input.into(); + let expected: ByteArray = "MMM"; + assert_eq!(output.into(), expected); +} + +#[test] +fn number_3001_is_mmmi() { + let input = 3001_u32; + let output: Roman = input.into(); + let expected: ByteArray = "MMMI"; + assert_eq!(output.into(), expected); +} + +#[test] +fn number_3888_is_mmmdccclxxxviii() { + let input = 3888_u32; + let output: Roman = input.into(); + let expected: ByteArray = "MMMDCCCLXXXVIII"; + assert_eq!(output.into(), expected); +} + +#[test] +fn number_3999_is_mmmcmxcix() { + let input = 3999_u32; + let output: Roman = input.into(); + let expected: ByteArray = "MMMCMXCIX"; + assert_eq!(output.into(), expected); +}