Created
November 7, 2023 13:56
-
-
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
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
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