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
Execute the tests with:
$ elixir scrabble_score_test.exs
In the test suites, all but the first test have been skipped.
Once you get a test passing, you can unskip the next one by
commenting out the relevant @tag :pending
with a #
symbol.
For example:
# @tag :pending
test "shouting" do
assert Bob.hey("WATCH OUT!") == "Whoa, chill out!"
end
Or, you can enable all the tests by commenting out the
ExUnit.configure
line in the test suite.
# ExUnit.configure exclude: :pending, trace: true
If you're stuck on something, it may help to look at some of the available resources out there where answers might be found.
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.
if !System.get_env("EXERCISM_TEST_EXAMPLES") do
Code.load_file("scrabble.exs", __DIR__)
end
ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)
defmodule ScrabbleScoreTest do
use ExUnit.Case
# @tag :pending
test "empty word scores zero" do
assert Scrabble.score("") == 0
end
@tag :pending
test "whitespace scores zero" do
assert Scrabble.score(" \t\n") == 0
end
@tag :pending
test "scores very short word" do
assert Scrabble.score("a") == 1
end
@tag :pending
test "scores other very short word" do
assert Scrabble.score("f") == 4
end
@tag :pending
test "simple word scores the number of letters" do
assert Scrabble.score("street") == 6
end
@tag :pending
test "complicated word scores more" do
assert Scrabble.score("quirky") == 22
end
@tag :pending
test "scores are case insensitive" do
assert Scrabble.score("OXYPHENBUTAZONE") == 41
end
@tag :pending
test "convenient scoring" do
assert Scrabble.score("alacrity") == 13
end
end
defmodule Scrabble do
@letter_values %{
"A" => 1, "E" => 1, "I" => 1, "O" => 1, "U" => 1, "L" => 1, "N" => 1, "R" => 1, "S" => 1, "T" => 1,
"D" => 2, "G" => 2,
"B" => 3, "C" => 3, "M" => 3, "P" => 3,
"F" => 4, "H" => 4, "V" => 4, "W" => 4, "Y" => 4,
"K" => 5,
"J" => 8, "X" => 8,
"Q" => 10, "Z" => 10,
}
@spec score(String.t()) :: non_neg_integer
def score(""),
do: 0
def score(<<letter::binary-size(1)>>),
do: Map.get(@letter_values, String.upcase(letter), 0)
def score(<<letter::binary-size(1), rest::binary()>>),
do: Map.get(@letter_values, String.upcase(letter), 0) + score(rest)
end
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.
Level up your programming skills with 3,449 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments