Skip to content

Commit

Permalink
Add "Mutability" concept + Simplify roman-numerals (#55)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Nenad Misić authored Jul 2, 2024
1 parent 4bd3bed commit ad68c09
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 112 deletions.
7 changes: 7 additions & 0 deletions concepts/mutability/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"blurb": "<todo>",
"authors": [
"misicnenad"
],
"contributors": []
}
1 change: 1 addition & 0 deletions concepts/mutability/about.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Mutability
1 change: 1 addition & 0 deletions concepts/mutability/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Introduction
1 change: 1 addition & 0 deletions concepts/mutability/links.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
14 changes: 10 additions & 4 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -190,7 +190,8 @@
"practices": [
"traits",
"strings",
"arrays"
"arrays",
"mutability"
],
"prerequisites": [],
"difficulty": 5
Expand Down Expand Up @@ -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": [
Expand Down
26 changes: 10 additions & 16 deletions exercises/practice/roman-numerals/.meta/example.cairo
Original file line number Diff line number Diff line change
@@ -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<u32, Roman> {
#[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![
Expand All @@ -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 {
Expand All @@ -38,17 +40,9 @@ impl RomanImpl of RomanTrait {
}
}

impl U32IntoRoman of Into<u32, Roman> {
#[must_use]
fn into(self: u32) -> Roman {
RomanImpl::from_u32(self)
}
}

impl RomanIIntoByteArray of Into<Roman, ByteArray> {
#[must_use]
fn into(self: Roman) -> ByteArray {
self.value
impl RomanDisplay of Display<Roman> {
fn fmt(self: @Roman, ref f: Formatter) -> Result<(), Error> {
write!(f, "{}", self.value)
}
}

Expand Down
16 changes: 5 additions & 11 deletions exercises/practice/roman-numerals/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -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<u32, Roman> {
#[must_use]
Expand All @@ -15,9 +10,8 @@ impl U32IntoRoman of Into<u32, Roman> {
}
}

impl RomanIIntoByteArray of Into<Roman, ByteArray> {
#[must_use]
fn into(self: Roman) -> ByteArray {
impl RomanDisplay of Display<Roman> {
fn fmt(self: @Roman, ref f: Formatter) -> Result<(), Error> {
panic!()
}
}
Expand Down
135 changes: 54 additions & 81 deletions exercises/practice/roman-numerals/src/tests.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit ad68c09

Please sign in to comment.