From a4be9acc800b9c4cbf1a82109c72fa2191406c98 Mon Sep 17 00:00:00 2001 From: Nenad Date: Wed, 19 Jun 2024 09:24:48 +0200 Subject: [PATCH] Rework armstrong num exercise to practice external crates --- concepts/external-crates/.meta/config.json | 7 ++++ concepts/external-crates/about.md | 1 + concepts/external-crates/introduction.md | 1 + concepts/external-crates/links.json | 1 + config.json | 12 +++++-- .../armstrong-numbers/.meta/example.cairo | 36 ++----------------- .../practice/armstrong-numbers/Scarb.toml | 3 ++ .../.docs/instructions.md | 0 .../semi-structured-logs/.meta/config.json | 16 +++++++++ .../semi-structured-logs/.meta/example.cairo | 0 .../practice/semi-structured-logs/Scarb.toml | 0 .../semi-structured-logs/src/lib.cairo | 0 .../semi-structured-logs/src/tests.cairo | 0 13 files changed, 41 insertions(+), 36 deletions(-) create mode 100644 concepts/external-crates/.meta/config.json create mode 100644 concepts/external-crates/about.md create mode 100644 concepts/external-crates/introduction.md create mode 100644 concepts/external-crates/links.json create mode 100644 exercises/practice/semi-structured-logs/.docs/instructions.md create mode 100644 exercises/practice/semi-structured-logs/.meta/config.json create mode 100644 exercises/practice/semi-structured-logs/.meta/example.cairo create mode 100644 exercises/practice/semi-structured-logs/Scarb.toml create mode 100644 exercises/practice/semi-structured-logs/src/lib.cairo create mode 100644 exercises/practice/semi-structured-logs/src/tests.cairo diff --git a/concepts/external-crates/.meta/config.json b/concepts/external-crates/.meta/config.json new file mode 100644 index 00000000..52914445 --- /dev/null +++ b/concepts/external-crates/.meta/config.json @@ -0,0 +1,7 @@ +{ + "blurb": "", + "authors": [ + "misicnenad" + ], + "contributors": [] +} diff --git a/concepts/external-crates/about.md b/concepts/external-crates/about.md new file mode 100644 index 00000000..a6639eed --- /dev/null +++ b/concepts/external-crates/about.md @@ -0,0 +1 @@ +# Packages and Crates diff --git a/concepts/external-crates/introduction.md b/concepts/external-crates/introduction.md new file mode 100644 index 00000000..e10b99d0 --- /dev/null +++ b/concepts/external-crates/introduction.md @@ -0,0 +1 @@ +# Introduction diff --git a/concepts/external-crates/links.json b/concepts/external-crates/links.json new file mode 100644 index 00000000..fe51488c --- /dev/null +++ b/concepts/external-crates/links.json @@ -0,0 +1 @@ +[] diff --git a/config.json b/config.json index 52fb5b8f..cdcd236a 100644 --- a/config.json +++ b/config.json @@ -62,8 +62,16 @@ "name": "Armstrong Numbers", "uuid": "4aaa153d-3416-4101-8143-35b068a15451", "practices": [ - "functions", - "integers", + "external-crates" + ], + "prerequisites": [], + "difficulty": 1 + }, + { + "slug": "semi-structured-logs", + "name": "semi-structured-logs", + "uuid": "2b0cd2a4-4767-4494-a239-7b3c0e9b0268", + "practices": [ "enums" ], "prerequisites": [], diff --git a/exercises/practice/armstrong-numbers/.meta/example.cairo b/exercises/practice/armstrong-numbers/.meta/example.cairo index 4c426fd5..edcaa330 100644 --- a/exercises/practice/armstrong-numbers/.meta/example.cairo +++ b/exercises/practice/armstrong-numbers/.meta/example.cairo @@ -1,39 +1,7 @@ -use core::integer::{u32_overflowing_add}; +use alexandria_math::armstrong_number; fn is_armstrong_number(num: u32) -> bool { - let s = format!("{}", num); - let l = s.len(); - let mut result: Option = Option::Some(0); - let mut i = l; - while i != 0 { - if let Option::None = result { - break; - } - - i -= 1; - - // ByteArray.at returns the ASCII representation of the character, - // so we need to subtract the position of the decimal '0' in ASCII - // (ASCII 48 == 0) - let digit: u8 = s.at(i).into().unwrap() - 48; - - result = match u32_overflowing_add(result.unwrap(), pow(digit, l)) { - Result::Ok(val) => Option::Some(val), - Result::Err(_) => Option::None, - }; - }; - - result == Option::Some(num) -} - -fn pow(base: u8, exponent: u32) -> u32 { - let mut result: u32 = 1; - let mut i = exponent; - while i != 0 { - result *= base.into(); // cast u8 into u32 - i -= 1; - }; - result + armstrong_number::is_armstrong_number(num.into()) } #[cfg(test)] diff --git a/exercises/practice/armstrong-numbers/Scarb.toml b/exercises/practice/armstrong-numbers/Scarb.toml index 18fcc7f8..380f4c75 100644 --- a/exercises/practice/armstrong-numbers/Scarb.toml +++ b/exercises/practice/armstrong-numbers/Scarb.toml @@ -2,3 +2,6 @@ name = "armstrong_numbers" version = "0.1.0" edition = "2023_11" + +[dependencies] +alexandria_math = { git = "https://github.com/keep-starknet-strange/alexandria.git" } \ No newline at end of file diff --git a/exercises/practice/semi-structured-logs/.docs/instructions.md b/exercises/practice/semi-structured-logs/.docs/instructions.md new file mode 100644 index 00000000..e69de29b diff --git a/exercises/practice/semi-structured-logs/.meta/config.json b/exercises/practice/semi-structured-logs/.meta/config.json new file mode 100644 index 00000000..bf076df5 --- /dev/null +++ b/exercises/practice/semi-structured-logs/.meta/config.json @@ -0,0 +1,16 @@ +{ + "authors": [], + "files": { + "solution": [ + "src/lib.cairo", + "Scarb.toml" + ], + "test": [ + "src/tests.cairo" + ], + "example": [ + ".meta/example.cairo" + ] + }, + "blurb": "" +} diff --git a/exercises/practice/semi-structured-logs/.meta/example.cairo b/exercises/practice/semi-structured-logs/.meta/example.cairo new file mode 100644 index 00000000..e69de29b diff --git a/exercises/practice/semi-structured-logs/Scarb.toml b/exercises/practice/semi-structured-logs/Scarb.toml new file mode 100644 index 00000000..e69de29b diff --git a/exercises/practice/semi-structured-logs/src/lib.cairo b/exercises/practice/semi-structured-logs/src/lib.cairo new file mode 100644 index 00000000..e69de29b diff --git a/exercises/practice/semi-structured-logs/src/tests.cairo b/exercises/practice/semi-structured-logs/src/tests.cairo new file mode 100644 index 00000000..e69de29b