Skip to content

Instantly share code, notes, and snippets.

@wperron
Created November 7, 2023 13:56
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 wperron/7e78b60003162b183347e17af621d3df to your computer and use it in GitHub Desktop.
Save wperron/7e78b60003162b183347e17af621d3df to your computer and use it in GitHub Desktop.
Given a list of words and a dictionary of letter scores, find the word with the highest score
fn main() {
let highest = vec!["apple", "banana", "cherry", "date", "fig"]
.into_iter()
.map(|w| (w, score(w.to_owned())))
.reduce(|acc, b| {
if b.1 > acc.1 {
return b;
}
acc
})
.unwrap();
println!("{}: {}", highest.0, highest.1);
}
fn score(word: String) -> u32 {
word.chars()
.into_iter()
.map(|c| c.to_ascii_lowercase() as u32 - 96)
.sum::<u32>()
* word.len() as u32
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment