Skip to content

Commit

Permalink
Merge pull request exercism#125 from IanWhitney/scrabble_score
Browse files Browse the repository at this point in the history
Add Scrabble Score
  • Loading branch information
IanWhitney committed May 25, 2016
2 parents c06ae40 + a5ac22d commit 83e4c0b
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 0 deletions.
1 change: 1 addition & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"rna-transcription",
"nucleotide-count",
"nucleotide-codons",
"scrabble-score",
"roman-numerals",
"robot-name",
"etl",
Expand Down
4 changes: 4 additions & 0 deletions exercises/scrabble-score/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions exercises/scrabble-score/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[package]
name = "scrabble-score"
version = "0.0.0"
40 changes: 40 additions & 0 deletions exercises/scrabble-score/example.rs
Original file line number Diff line number Diff line change
@@ -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<char, u16> {
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
}
63 changes: 63 additions & 0 deletions exercises/scrabble-score/tests/scrabble-score.rs
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 83e4c0b

Please sign in to comment.