ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# ajborla's solution

## to Scrabble Score in the Erlang Track

Published at Aug 24 2019 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution was written.

Given a word, compute the scrabble score for that word.

## Letter Values

You'll need these:

``````Letter                           Value
A, E, I, O, U, L, N, R, S, T       1
D, G                               2
B, C, M, P                         3
F, H, V, W, Y                      4
K                                  5
J, X                               8
Q, Z                               10
``````

## Examples

"cabbage" should be scored as worth 14 points:

• 3 points for C
• 1 point for A, twice
• 3 points for B, twice
• 2 points for G
• 1 point for E

And to total:

• `3 + 2*1 + 2*3 + 2 + 1`
• = `3 + 2 + 6 + 3`
• = `5 + 9`
• = 14

## Extensions

• You can play a double or a triple letter.
• You can play a double or a triple word.

## Running tests

In order to run the tests, issue the following command from the exercise directory:

For running the tests provided, `rebar3` is used as it is the official build and dependency management tool for erlang now. Please refer to the tracks installation instructions on how to do that.

In order to run the tests, you can issue the following command from the exercise directory.

``````\$ rebar3 eunit
``````

## Questions?

For detailed information about the Erlang track, please refer to the help page on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.

## Source

Inspired by the Extreme Startup game https://github.com/rchatley/extreme_startup

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### scrabble_score_tests.erl

``````%% Based on canonical data version 1.1.0
%% https://github.com/exercism/problem-specifications/raw/master/exercises/scrabble-score/canonical-data.json
%% This file is automatically generated from the exercises canonical data.

-module(scrabble_score_tests).

-include_lib("erl_exercism/include/exercism.hrl").
-include_lib("eunit/include/eunit.hrl").

'1_lowercase_letter_test'() ->
?assertEqual(1, scrabble_score:score("a")).

'2_uppercase_letter_test'() ->
?assertEqual(1, scrabble_score:score("A")).

'3_valuable_letter_test'() ->
?assertEqual(4, scrabble_score:score("f")).

'4_short_word_test'() ->
?assertEqual(2, scrabble_score:score("at")).

'5_short_valuable_word_test'() ->
?assertEqual(12, scrabble_score:score("zoo")).

'6_medium_word_test'() ->
?assertEqual(6, scrabble_score:score("street")).

'7_medium_valuable_word_test'() ->
?assertEqual(22, scrabble_score:score("quirky")).

'8_long_mixed_case_word_test'() ->
?assertEqual(41,
scrabble_score:score("OxyphenButazone")).

'9_english_like_word_test'() ->
?assertEqual(8, scrabble_score:score("pinata")).

'10_empty_input_test'() ->
?assertEqual(0, scrabble_score:score([])).

'11_entire_alphabet_available_test'() ->
?assertEqual(87,
scrabble_score:score("abcdefghijklmnopqrstuvwxyz")).``````
``````% -----------------------------------------------------------------------------
% exercism.io
% Erlang Track Exercise: scrabble_score
% Contributed: Anthony J. Borla (ajborla@bigpond.com)
% -----------------------------------------------------------------------------
-module(scrabble_score).
-export([score/1, version/0]).

%
% Scrabble Letter Value Mappings
%
-define(LETTER_VALUES, [
{\$a, 1}, {\$b, 3}, {\$c, 3}, {\$d, 2}, {\$e, 1}, {\$f, 4},
{\$g, 2}, {\$h, 4}, {\$i, 1}, {\$j, 8}, {\$k, 5}, {\$l, 1},
{\$m, 3}, {\$n, 1}, {\$o, 1}, {\$p, 3}, {\$q, 10},{\$r, 1},
{\$s, 1}, {\$t, 1}, {\$u, 1}, {\$v, 4}, {\$w, 4}, {\$x, 8},
{\$y, 4}, {\$z, 10} ]).

% -----------------------------------------------------------------------------
% Given a word, 'Word', returns the Scrabble score for the word. Note:
% - Zero-length word scores a zero
% - No validation performed on word - assumed it comprises letters only
% - Replacing tuple list with Maps (maps module) may improve lookup performance
% -----------------------------------------------------------------------------
-spec score(string()) -> non_neg_integer().
score([]) -> 0;
score(Word) ->
% Create 'scorer' function to return score for a given letter
Scorer = fun (Letter, Sum) ->
{_, Score} = lists:keyfind(Letter, 1, ?LETTER_VALUES),
Score + Sum
end,

% Apply scorer function to each letter, sum score, return word score
lists:foldl(Scorer, 0, string:to_lower(Word)).

% -----------------------------------------------------------------------------
% Returns module version string in MAJOR.MINOR.REVISION format
% -----------------------------------------------------------------------------
-spec version() -> string().
version() -> "1.0.0".``````