Created
July 8, 2023 07:53
-
-
Save dustinknopoff/45afdab7c652d2eab402c418e7b2c723 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Lowercase letters start at 97 => 1 | |
// Max is 122 for z => 26 | |
pub fn ascii_sum(string: &'static str) -> u32 { | |
string.bytes().map(|char| { | |
(char - 96) as u32 | |
}).sum::<u32>() | |
} | |
fn main() { | |
println!("helloworld") | |
} | |
#[cfg(test)] | |
mod tests { | |
use super::ascii_sum; | |
#[test] | |
pub fn empty() { | |
assert_eq!(ascii_sum(""), 0) | |
} | |
#[test] | |
pub fn a() { | |
assert_eq!(ascii_sum("a"), 1) | |
} | |
#[test] | |
pub fn z() { | |
assert_eq!(ascii_sum("z"), 26) | |
} | |
#[test] | |
pub fn cab() { | |
assert_eq!(ascii_sum("cab"), 6) | |
} | |
#[test] | |
pub fn excellent() { | |
assert_eq!(ascii_sum("excellent"), 100) | |
} | |
#[test] | |
pub fn long() { | |
assert_eq!(ascii_sum("microspectrophotometries"), 317) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment