From b8a1a0cf13b740592bad6177325abaf91e24a7eb Mon Sep 17 00:00:00 2001 From: William Perron Date: Tue, 7 Nov 2023 08:56:06 -0500 Subject: [PATCH] add word score game (issue #325) --- Cargo.lock | 4 ++++ Cargo.toml | 3 ++- word-score/Cargo.toml | 9 +++++++++ word-score/src/main.rs | 21 +++++++++++++++++++++ 4 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 word-score/Cargo.toml create mode 100644 word-score/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index 9543680..666be4e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml index a45516b..270fac7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,5 +7,6 @@ members = [ "parens", "repeated-groups", "scrabble", - "scramble" + "scramble", + "word-score" ] diff --git a/word-score/Cargo.toml b/word-score/Cargo.toml new file mode 100644 index 0000000..4d9ea5b --- /dev/null +++ b/word-score/Cargo.toml @@ -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] + diff --git a/word-score/src/main.rs b/word-score/src/main.rs new file mode 100644 index 0000000..83db1bf --- /dev/null +++ b/word-score/src/main.rs @@ -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::() + * word.len() as u32 +}