feat(rust): add persistent bugger
This commit is contained in:
parent
069972f5d5
commit
65014a1f22
4 changed files with 40 additions and 0 deletions
1
rust/.gitignore
vendored
Normal file
1
rust/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
target/
|
7
rust/Cargo.lock
generated
Normal file
7
rust/Cargo.lock
generated
Normal file
|
@ -0,0 +1,7 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "rust"
|
||||
version = "0.1.0"
|
8
rust/Cargo.toml
Normal file
8
rust/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "rust"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
24
rust/tests/1_persistent_bugger.rs
Normal file
24
rust/tests/1_persistent_bugger.rs
Normal file
|
@ -0,0 +1,24 @@
|
|||
fn persistence(num: u64) -> u64 {
|
||||
match num {
|
||||
0..=9 => 0,
|
||||
_ => {
|
||||
let res = num
|
||||
.to_string()
|
||||
.chars()
|
||||
.map(|c| c.to_digit(10).unwrap() as u64)
|
||||
.product();
|
||||
1 + persistence(res)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
fn sample_tests() {
|
||||
assert_eq!(super::persistence(39), 3);
|
||||
assert_eq!(super::persistence(4), 0);
|
||||
assert_eq!(super::persistence(25), 2);
|
||||
assert_eq!(super::persistence(999), 4);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue