From a5ac22d4686fede69e5be51a71351139893913b6 Mon Sep 17 00:00:00 2001 From: Ian Whitney Date: Tue, 17 May 2016 12:43:48 -0500 Subject: [PATCH] Add Scrabble Score Implements the Scrabble Score exercise. As of right now there is no official test suite for Scrabble Score. There is a PR (https://github.com/exercism/x-common/pull/161), but it is not yet merged. I've put this exercise between the Nucleotides and Roman Numerals, figuring that it did similar character iteration and value mapping. I am following the test suite in that PR, with one exception. I thought it was likely that a common naive implementation would be to map through the chars, find the score and then simply unwrap the value. Like this ``` word.chars().map(|c| values.get(&c).fold(0, |sum, v| sum + v.unwrap()) ``` So I have added a test that uses a non-English character that will cause this implementation to panic. My example implementation probably leaves a lot to be desired. I wanted to make the values hash a static, but you can't do that with a hash. ([Lazy Static](http://rust-lang-nursery.github.io/lazy-static.rs/lazy_static/index.html) does let you do this, but I'm not going to require a crate in my example). --- config.json | 1 + exercises/scrabble-score/Cargo.lock | 4 ++ exercises/scrabble-score/Cargo.toml | 3 + exercises/scrabble-score/example.rs | 40 ++++++++++++ .../scrabble-score/tests/scrabble-score.rs | 63 +++++++++++++++++++ 5 files changed, 111 insertions(+) create mode 100644 exercises/scrabble-score/Cargo.lock create mode 100644 exercises/scrabble-score/Cargo.toml create mode 100644 exercises/scrabble-score/example.rs create mode 100644 exercises/scrabble-score/tests/scrabble-score.rs diff --git a/config.json b/config.json index df00182e2..e21181b55 100644 --- a/config.json +++ b/config.json @@ -15,6 +15,7 @@ "rna-transcription", "nucleotide-count", "nucleotide-codons", + "scrabble-score", "roman-numerals", "robot-name", "etl", diff --git a/exercises/scrabble-score/Cargo.lock b/exercises/scrabble-score/Cargo.lock new file mode 100644 index 000000000..f78bd5d2c --- /dev/null +++ b/exercises/scrabble-score/Cargo.lock @@ -0,0 +1,4 @@ +[root] +name = "scrabble-score" +version = "0.0.0" + diff --git a/exercises/scrabble-score/Cargo.toml b/exercises/scrabble-score/Cargo.toml new file mode 100644 index 000000000..2befd6321 --- /dev/null +++ b/exercises/scrabble-score/Cargo.toml @@ -0,0 +1,3 @@ +[package] +name = "scrabble-score" +version = "0.0.0" diff --git a/exercises/scrabble-score/example.rs b/exercises/scrabble-score/example.rs new file mode 100644 index 000000000..4a373dc2f --- /dev/null +++ b/exercises/scrabble-score/example.rs @@ -0,0 +1,40 @@ +use std::collections::HashMap; + +pub fn score(word: &str) -> u16 { + let values = dictionary(); + word.to_lowercase() + .chars() + .map(|c| values.get(&c).clone()) + .fold(0, |score, v| score + v.unwrap_or(&0)) +} + +fn dictionary() -> HashMap { + let mut values = HashMap::new(); + values.insert('a', 1); + values.insert('b', 3); + values.insert('c', 3); + values.insert('d', 2); + values.insert('e', 1); + values.insert('f', 4); + values.insert('g', 2); + values.insert('h', 4); + values.insert('i', 1); + values.insert('j', 8); + values.insert('k', 5); + values.insert('l', 1); + values.insert('m', 3); + values.insert('n', 1); + values.insert('o', 1); + values.insert('p', 3); + values.insert('q', 10); + values.insert('r', 1); + values.insert('s', 1); + values.insert('t', 1); + values.insert('u', 1); + values.insert('v', 4); + values.insert('w', 4); + values.insert('x', 8); + values.insert('y', 4); + values.insert('z', 10); + values +} diff --git a/exercises/scrabble-score/tests/scrabble-score.rs b/exercises/scrabble-score/tests/scrabble-score.rs new file mode 100644 index 000000000..caa777266 --- /dev/null +++ b/exercises/scrabble-score/tests/scrabble-score.rs @@ -0,0 +1,63 @@ +extern crate scrabble_score; + +use scrabble_score::*; + +#[test] +fn a_is_worth_one_point() { + assert_eq!(score("a"), 1); +} + +#[test] +#[ignore] +fn scoring_is_case_insensitive() { + assert_eq!(score("A"), 1); +} + +#[test] +#[ignore] +fn f_is_worth_four() { + assert_eq!(score("f"), 4); +} + +#[test] +#[ignore] +fn two_one_point_letters_make_a_two_point_word() { + assert_eq!(score("at"), 2); +} + +#[test] +#[ignore] +fn three_letter_word() { + assert_eq!(score("zoo"), 12); +} + +#[test] +#[ignore] +fn medium_word() { + assert_eq!(score("street"), 6); +} + +#[test] +#[ignore] +fn longer_words_with_valuable_letters() { + assert_eq!(score("quirky"), 22); +} + +#[test] +#[ignore] +fn long_mixed_case_word() { + assert_eq!(score("OxyphenButazone"), 41); +} + +#[test] +#[ignore] +fn non_english_scrabble_letters_do_not_score() { + assert_eq!(score("pinata"), 8); + assert_eq!(score("piƱata"), 7); +} + +#[test] +#[ignore] +fn empty_words_are_worth_zero() { + assert_eq!(score(""), 0); +}