scrabble-score
This commit is contained in:
21
elixir/scrabble-score/lib/scrabble.ex
Normal file
21
elixir/scrabble-score/lib/scrabble.ex
Normal file
@@ -0,0 +1,21 @@
|
||||
defmodule Scrabble do
|
||||
defp char_score(char) when char in ~w(A E I O U L N R S T), do: 1
|
||||
defp char_score(char) when char in ~w(D G), do: 2
|
||||
defp char_score(char) when char in ~w(B C M P), do: 3
|
||||
defp char_score(char) when char in ~w(F H V W Y), do: 4
|
||||
defp char_score(char) when char in ~w(K), do: 5
|
||||
defp char_score(char) when char in ~w(J X), do: 8
|
||||
defp char_score(char) when char in ~w(Q Z), do: 10
|
||||
defp char_score(_char), do: 0
|
||||
|
||||
@doc """
|
||||
Calculate the scrabble score for the word.
|
||||
"""
|
||||
@spec score(String.t()) :: non_neg_integer
|
||||
def score(word) do
|
||||
word
|
||||
|> String.upcase()
|
||||
|> String.graphemes()
|
||||
|> Enum.reduce(0, fn char, score -> score + char_score(char) end)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user