From ad68c09c0b1b4f68a4f797b16ef8fcdd67977753 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nenad=20Misi=C4=87?= Date: Tue, 2 Jul 2024 12:53:23 +0200 Subject: [PATCH] Add "Mutability" concept + Simplify `roman-numerals` (#55) * remove 'from_u32' * Simplify roman-numerals * Add mutability concept to roman-numerals + update difficulty to 3 * add mutability to anagram * add scaffold to lib.cairo --- concepts/mutability/.meta/config.json | 7 + concepts/mutability/about.md | 1 + concepts/mutability/introduction.md | 1 + concepts/mutability/links.json | 1 + config.json | 14 +- .../roman-numerals/.meta/example.cairo | 26 ++-- .../practice/roman-numerals/src/lib.cairo | 16 +-- .../practice/roman-numerals/src/tests.cairo | 135 +++++++----------- 8 files changed, 89 insertions(+), 112 deletions(-) create mode 100644 concepts/mutability/.meta/config.json create mode 100644 concepts/mutability/about.md create mode 100644 concepts/mutability/introduction.md create mode 100644 concepts/mutability/links.json diff --git a/concepts/mutability/.meta/config.json b/concepts/mutability/.meta/config.json new file mode 100644 index 00000000..52914445 --- /dev/null +++ b/concepts/mutability/.meta/config.json @@ -0,0 +1,7 @@ +{ + "blurb": "", + "authors": [ + "misicnenad" + ], + "contributors": [] +} diff --git a/concepts/mutability/about.md b/concepts/mutability/about.md new file mode 100644 index 00000000..b2197b1c --- /dev/null +++ b/concepts/mutability/about.md @@ -0,0 +1 @@ +# Mutability diff --git a/concepts/mutability/introduction.md b/concepts/mutability/introduction.md new file mode 100644 index 00000000..e10b99d0 --- /dev/null +++ b/concepts/mutability/introduction.md @@ -0,0 +1 @@ +# Introduction diff --git a/concepts/mutability/links.json b/concepts/mutability/links.json new file mode 100644 index 00000000..fe51488c --- /dev/null +++ b/concepts/mutability/links.json @@ -0,0 +1 @@ +[] diff --git a/config.json b/config.json index 7f49d225..2bbc0f3a 100644 --- a/config.json +++ b/config.json @@ -129,12 +129,12 @@ "name": "Roman Numerals", "uuid": "3fb4bd2f-bc5f-4a67-b9cc-49a786245876", "practices": [ - "tuples", "traits", - "type-conversion" + "type-conversion", + "mutability" ], "prerequisites": [], - "difficulty": 2 + "difficulty": 3 }, { "slug": "binary-search", @@ -190,7 +190,8 @@ "practices": [ "traits", "strings", - "arrays" + "arrays", + "mutability" ], "prerequisites": [], "difficulty": 5 @@ -286,6 +287,11 @@ "uuid": "fe81e75b-b4d2-4ee8-9aaa-271e76468d0b", "slug": "printing", "name": "Printing" + }, + { + "uuid": "8a4a6525-c1e4-44f5-ab6b-cf5d8ce6701e", + "slug": "mutability", + "name": "M`utability" } ], "key_features": [ diff --git a/exercises/practice/roman-numerals/.meta/example.cairo b/exercises/practice/roman-numerals/.meta/example.cairo index 3cfcad08..e62e2720 100644 --- a/exercises/practice/roman-numerals/.meta/example.cairo +++ b/exercises/practice/roman-numerals/.meta/example.cairo @@ -1,11 +1,13 @@ -#[derive(Drop, Debug, PartialEq)] +use core::fmt::{Display, Error, Formatter}; + +#[derive(Drop)] pub struct Roman { value: ByteArray, } -#[generate_trait] -impl RomanImpl of RomanTrait { - fn from_u32(num: u32) -> Roman { +impl U32IntoRoman of Into { + #[must_use] + fn into(self: 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![ @@ -25,7 +27,7 @@ impl RomanImpl of RomanTrait { ]; let mut value: ByteArray = ""; - let mut current_num = num; + let mut current_num = self; while let Option::Some((numeric, roman_string)) = ROMAN_MAP .pop_front() { while current_num >= numeric { @@ -38,17 +40,9 @@ impl RomanImpl of RomanTrait { } } -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 +impl RomanDisplay of Display { + fn fmt(self: @Roman, ref f: Formatter) -> Result<(), Error> { + write!(f, "{}", self.value) } } diff --git a/exercises/practice/roman-numerals/src/lib.cairo b/exercises/practice/roman-numerals/src/lib.cairo index 1433e68b..d9fb1718 100644 --- a/exercises/practice/roman-numerals/src/lib.cairo +++ b/exercises/practice/roman-numerals/src/lib.cairo @@ -1,12 +1,7 @@ -#[derive(Drop, Debug, PartialEq)] -pub struct Roman {} +use core::fmt::{Display, Error, Formatter}; -#[generate_trait] -impl RomanImpl of RomanTrait { - fn new(num: u32) -> Roman { - panic!() - } -} +#[derive(Drop)] +pub struct Roman {} impl U32IntoRoman of Into { #[must_use] @@ -15,9 +10,8 @@ impl U32IntoRoman of Into { } } -impl RomanIIntoByteArray of Into { - #[must_use] - fn into(self: Roman) -> ByteArray { +impl RomanDisplay of Display { + fn fmt(self: @Roman, ref f: Formatter) -> Result<(), Error> { panic!() } } diff --git a/exercises/practice/roman-numerals/src/tests.cairo b/exercises/practice/roman-numerals/src/tests.cairo index 843890ae..df93c7de 100644 --- a/exercises/practice/roman-numerals/src/tests.cairo +++ b/exercises/practice/roman-numerals/src/tests.cairo @@ -2,216 +2,189 @@ use roman_numerals::Roman; #[test] fn number_1_is_i() { - let input = 1_u32; - let output: Roman = input.into(); + let output: Roman = 1_u32.into(); let expected: ByteArray = "I"; - assert_eq!(output.into(), expected); + assert_eq!(format!("{output}"), expected); } #[test] fn number_2_is_ii() { - let input = 2_u32; - let output: Roman = input.into(); + let output: Roman = 2_u32.into(); let expected: ByteArray = "II"; - assert_eq!(output.into(), expected); + assert_eq!(format!("{output}"), expected); } #[test] fn number_3_is_iii() { - let input = 3_u32; - let output: Roman = input.into(); + let output: Roman = 3_u32.into(); let expected: ByteArray = "III"; - assert_eq!(output.into(), expected); + assert_eq!(format!("{output}"), expected); } #[test] fn number_4_is_iv() { - let input = 4_u32; - let output: Roman = input.into(); + let output: Roman = 4_u32.into(); let expected: ByteArray = "IV"; - assert_eq!(output.into(), expected); + assert_eq!(format!("{output}"), expected); } #[test] fn number_5_is_v() { - let input = 5_u32; - let output: Roman = input.into(); + let output: Roman = 5_u32.into(); let expected: ByteArray = "V"; - assert_eq!(output.into(), expected); + assert_eq!(format!("{output}"), expected); } #[test] fn number_6_is_vi() { - let input = 6_u32; - let output: Roman = input.into(); + let output: Roman = 6_u32.into(); let expected: ByteArray = "VI"; - assert_eq!(output.into(), expected); + assert_eq!(format!("{output}"), expected); } #[test] fn number_9_is_ix() { - let input = 9_u32; - let output: Roman = input.into(); + let output: Roman = 9_u32.into(); let expected: ByteArray = "IX"; - assert_eq!(output.into(), expected); + assert_eq!(format!("{output}"), expected); } #[test] fn number_16_is_xvi() { - let input = 16_u32; - let output: Roman = input.into(); + let output: Roman = 16_u32.into(); let expected: ByteArray = "XVI"; - assert_eq!(output.into(), expected); + assert_eq!(format!("{output}"), expected); } #[test] fn number_27_is_xxvii() { - let input = 27_u32; - let output: Roman = input.into(); + let output: Roman = 27_u32.into(); let expected: ByteArray = "XXVII"; - assert_eq!(output.into(), expected); + assert_eq!(format!("{output}"), expected); } #[test] fn number_48_is_xlviii() { - let input = 48_u32; - let output: Roman = input.into(); + let output: Roman = 48_u32.into(); let expected: ByteArray = "XLVIII"; - assert_eq!(output.into(), expected); + assert_eq!(format!("{output}"), expected); } #[test] fn number_49_is_xlix() { - let input = 49_u32; - let output: Roman = input.into(); + let output: Roman = 49_u32.into(); let expected: ByteArray = "XLIX"; - assert_eq!(output.into(), expected); + assert_eq!(format!("{output}"), expected); } #[test] fn number_59_is_lix() { - let input = 59_u32; - let output: Roman = input.into(); + let output: Roman = 59_u32.into(); let expected: ByteArray = "LIX"; - assert_eq!(output.into(), expected); + assert_eq!(format!("{output}"), expected); } #[test] fn number_66_is_lxvi() { - let input = 66_u32; - let output: Roman = input.into(); + let output: Roman = 66_u32.into(); let expected: ByteArray = "LXVI"; - assert_eq!(output.into(), expected); + assert_eq!(format!("{output}"), expected); } #[test] fn number_93_is_xciii() { - let input = 93_u32; - let output: Roman = input.into(); + let output: Roman = 93_u32.into(); let expected: ByteArray = "XCIII"; - assert_eq!(output.into(), expected); + assert_eq!(format!("{output}"), expected); } #[test] fn number_141_is_cxli() { - let input = 141_u32; - let output: Roman = input.into(); + let output: Roman = 141_u32.into(); let expected: ByteArray = "CXLI"; - assert_eq!(output.into(), expected); + assert_eq!(format!("{output}"), expected); } #[test] fn number_163_is_clxiii() { - let input = 163_u32; - let output: Roman = input.into(); + let output: Roman = 163_u32.into(); let expected: ByteArray = "CLXIII"; - assert_eq!(output.into(), expected); + assert_eq!(format!("{output}"), expected); } #[test] fn number_166_is_clxvi() { - let input = 166_u32; - let output: Roman = input.into(); + let output: Roman = 166_u32.into(); let expected: ByteArray = "CLXVI"; - assert_eq!(output.into(), expected); + assert_eq!(format!("{output}"), expected); } #[test] fn number_402_is_cdii() { - let input = 402_u32; - let output: Roman = input.into(); + let output: Roman = 402_u32.into(); let expected: ByteArray = "CDII"; - assert_eq!(output.into(), expected); + assert_eq!(format!("{output}"), expected); } #[test] fn number_575_is_dlxxv() { - let input = 575_u32; - let output: Roman = input.into(); + let output: Roman = 575_u32.into(); let expected: ByteArray = "DLXXV"; - assert_eq!(output.into(), expected); + assert_eq!(format!("{output}"), expected); } #[test] fn number_666_is_dclxvi() { - let input = 666_u32; - let output: Roman = input.into(); + let output: Roman = 666_u32.into(); let expected: ByteArray = "DCLXVI"; - assert_eq!(output.into(), expected); + assert_eq!(format!("{output}"), expected); } #[test] fn number_911_is_cmxi() { - let input = 911_u32; - let output: Roman = input.into(); + let output: Roman = 911_u32.into(); let expected: ByteArray = "CMXI"; - assert_eq!(output.into(), expected); + assert_eq!(format!("{output}"), expected); } #[test] fn number_1024_is_mxxiv() { - let input = 1024_u32; - let output: Roman = input.into(); + let output: Roman = 1024_u32.into(); let expected: ByteArray = "MXXIV"; - assert_eq!(output.into(), expected); + assert_eq!(format!("{output}"), expected); } #[test] fn number_1666_is_mdclxvi() { - let input = 1666_u32; - let output: Roman = input.into(); + let output: Roman = 1666_u32.into(); let expected: ByteArray = "MDCLXVI"; - assert_eq!(output.into(), expected); + assert_eq!(format!("{output}"), expected); } #[test] fn number_3000_is_mmm() { - let input = 3000_u32; - let output: Roman = input.into(); + let output: Roman = 3000_u32.into(); let expected: ByteArray = "MMM"; - assert_eq!(output.into(), expected); + assert_eq!(format!("{output}"), expected); } #[test] fn number_3001_is_mmmi() { - let input = 3001_u32; - let output: Roman = input.into(); + let output: Roman = 3001_u32.into(); let expected: ByteArray = "MMMI"; - assert_eq!(output.into(), expected); + assert_eq!(format!("{output}"), expected); } #[test] fn number_3888_is_mmmdccclxxxviii() { - let input = 3888_u32; - let output: Roman = input.into(); + let output: Roman = 3888_u32.into(); let expected: ByteArray = "MMMDCCCLXXXVIII"; - assert_eq!(output.into(), expected); + assert_eq!(format!("{output}"), expected); } #[test] fn number_3999_is_mmmcmxcix() { - let input = 3999_u32; - let output: Roman = input.into(); + let output: Roman = 3999_u32.into(); let expected: ByteArray = "MMMCMXCIX"; - assert_eq!(output.into(), expected); + assert_eq!(format!("{output}"), expected); }