Skip to content

Commit

Permalink
Add more traits (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored Jan 10, 2022
1 parent 3cd18d7 commit c0cd471
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<String>());

Ok(())
}
39 changes: 39 additions & 0 deletions tests/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}

0 comments on commit c0cd471

Please sign in to comment.