From c0cd4715838377e5a4dd844d13e92f7636173130 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Mon, 10 Jan 2022 11:14:09 -0800 Subject: [PATCH] Add more traits (#25) --- src/traits.rs | 27 +++++++++++++++++++++++++++ tests/traits.rs | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/src/traits.rs b/src/traits.rs index df2c682e90..e86cc622d6 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -15,5 +15,32 @@ pub(crate) fn run(n: u64) -> Result { println!("odd"); } + let pi = std::f64::consts::PI.to_string().replace('.', ""); + let s = n.to_string(); + if s == pi[..s.len()] { + println!("pi"); + } + + if s.replace("69", "") == "" { + println!("nice"); + } + + if s.replace("7", "") == "" { + println!("angelic"); + } + + let mut name = String::new(); + let mut remainder = n; + while remainder > 0 { + name.push( + "abcdefghijklmnopqrstuvwxyz" + .chars() + .nth(((remainder - 1) % 26) as usize) + .unwrap(), + ); + remainder = (remainder - 1) / 26; + } + println!("name:{}", name.chars().rev().collect::()); + Ok(()) } diff --git a/tests/traits.rs b/tests/traits.rs index 6bac799591..97b9d210e1 100644 --- a/tests/traits.rs +++ b/tests/traits.rs @@ -42,3 +42,42 @@ fn odd() -> Result { assert!(!traits(2)?.contains("odd")); Ok(()) } + +#[test] +fn pi() -> Result { + assert!(!traits(0)?.contains("pi")); + assert!(traits(3)?.contains("pi")); + assert!(traits(31)?.contains("pi")); + assert!(traits(314)?.contains("pi")); + assert!(!traits(3145)?.contains("pi")); + Ok(()) +} + +#[test] +fn nice() -> Result { + assert!(!traits(0)?.contains("nice")); + assert!(traits(69)?.contains("nice")); + assert!(traits(6969)?.contains("nice")); + assert!(traits(696969)?.contains("nice")); + assert!(!traits(696968)?.contains("nice")); + Ok(()) +} + +#[test] +fn divine() -> Result { + assert!(!traits(0)?.contains("angelic")); + assert!(traits(7)?.contains("angelic")); + assert!(traits(77)?.contains("angelic")); + assert!(traits(777)?.contains("angelic")); + assert!(!traits(778)?.contains("angelic")); + Ok(()) +} + +#[test] +fn name() -> Result { + assert!(traits(0)?.contains("name:")); + assert!(traits(1)?.contains("name:a")); + assert!(traits(26)?.contains("name:z")); + assert!(traits(27)?.contains("name:aa")); + Ok(()) +}