Skip to content

Instantly share code, notes, and snippets.

@dustinknopoff
Created July 8, 2023 07:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dustinknopoff/45afdab7c652d2eab402c418e7b2c723 to your computer and use it in GitHub Desktop.
Save dustinknopoff/45afdab7c652d2eab402c418e7b2c723 to your computer and use it in GitHub Desktop.
// 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