add word score game (issue #325)
parent
93f04e0d98
commit
b8a1a0cf13
@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "word-score"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
|
@ -0,0 +1,21 @@
|
||||
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
|
||||
}
|
Loading…
Reference in new issue