add word score game (issue #325)

main
William Perron 1 year ago
parent 93f04e0d98
commit b8a1a0cf13
No known key found for this signature in database
GPG Key ID: D1815C43C9BA3DE1

4
Cargo.lock generated

@ -110,3 +110,7 @@ name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "word-score"
version = "0.1.0"

@ -7,5 +7,6 @@ members = [
"parens",
"repeated-groups",
"scrabble",
"scramble"
"scramble",
"word-score"
]

@ -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…
Cancel
Save