Given a word, compute the scrabble score for that word.
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
"cabbage" should be scored as worth 14 points:
And to total:
3 + 2*1 + 2*3 + 2 + 1
3 + 2 + 6 + 3
5 + 9
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
Each problem defines a macro
TEST_VERSION in the test file and
verifies that the solution defines and exports a function
returning that same value.
To make tests pass, add the following to your solution:
-export([test_version/0]). test_version() -> 1.
The benefit of this is that reviewers can see against which test version an iteration was written if, for example, a previously posted solution does not solve the current problem or passes current tests.
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.
Inspired by the Extreme Startup game https://github.com/rchatley/extreme_startup
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
-module(scrabble_score_tests). -include_lib("erl_exercism/include/exercism.hrl"). -include_lib("eunit/include/eunit.hrl"). -define(checkScore(N, S), ?assertEqual(N, scrabble_score:score(S))). empty_word_scores_zero_test() -> ?checkScore(0, ""). scores_very_short_word_test() -> ?checkScore(1, "a"). scores_other_very_short_word_test() -> ?checkScore(4, "f"). simple_word_scores_the_number_of_letters_test() -> ?checkScore(6, "street"). complicated_word_scores_more_test() -> ?checkScore(22, "quirky"). scores_are_case_insensitive_test() -> ?checkScore(41, "oxyphenbutazone"), ?checkScore(41, "OXYPHENBUTAZONE"). version_test() -> ?assertMatch(1, scrabble_score:test_version()).
-module(scrabble_score). -export([score/1, test_version/0, to_lowercase/1]). score("") -> 0; score(Word) -> LowercaseWord = [to_lowercase(W) || W <- Word], score(to_lowercase(LowercaseWord), 0). score(, Acc) -> Acc; score([H|T], Acc) when H == $a orelse H == $e orelse H == $i orelse H == $o orelse H == $u orelse H == $l orelse H == $n orelse H == $r orelse H == $s orelse H == $t -> score(T, Acc + 1); score([H|T], Acc) when H == $d orelse H == $g -> score(T, Acc + 2); score([H|T], Acc) when H == $b orelse H == $c orelse H == $m orelse H == $p -> score(T, Acc + 3); score([H|T], Acc) when H == $f orelse H == $h orelse H == $v orelse H == $w orelse H == $y -> score(T, Acc + 4); score([H|T], Acc) when H == $k -> score(T, Acc + 5); score([H|T], Acc) when H == $j orelse H == $x -> score(T, Acc + 8); score([H|T], Acc) when H == $q orelse H == $z -> score(T, Acc + 10); score([_H|T], Acc) -> score(T, Acc). test_version() -> 1. to_lowercase(H) when (H >= 65 andalso H =< 90) -> H + 32; to_lowercase(H) -> H.
A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.
Here are some questions to help you reflect on this solution and learn the most from it.