Skip to content

Commit

Permalink
Rework armstrong num exercise to practice external crates
Browse files Browse the repository at this point in the history
  • Loading branch information
Nenad committed Jun 19, 2024
1 parent 38aeb09 commit a4be9ac
Show file tree
Hide file tree
Showing 13 changed files with 41 additions and 36 deletions.
7 changes: 7 additions & 0 deletions concepts/external-crates/.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/external-crates/about.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Packages and Crates
1 change: 1 addition & 0 deletions concepts/external-crates/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Introduction
1 change: 1 addition & 0 deletions concepts/external-crates/links.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
12 changes: 10 additions & 2 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [],
Expand Down
36 changes: 2 additions & 34 deletions exercises/practice/armstrong-numbers/.meta/example.cairo
Original file line number Diff line number Diff line change
@@ -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<u32> = 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)]
Expand Down
3 changes: 3 additions & 0 deletions exercises/practice/armstrong-numbers/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Empty file.
16 changes: 16 additions & 0 deletions exercises/practice/semi-structured-logs/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"authors": [],
"files": {
"solution": [
"src/lib.cairo",
"Scarb.toml"
],
"test": [
"src/tests.cairo"
],
"example": [
".meta/example.cairo"
]
},
"blurb": ""
}
Empty file.
Empty file.
Empty file.
Empty file.

0 comments on commit a4be9ac

Please sign in to comment.