Created
December 8, 2015 05:12
-
-
Save terakilobyte/846096f7bbea9608b475 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule Scrabble do | |
| @doc """ | |
| Calculate the scrabble score for the word. | |
| """ | |
| @spec score(String.t) :: non_neg_integer | |
| @scrabble_score %{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} | |
| def score(word) do | |
| word | |
| |> String.strip | |
| |> String.downcase | |
| |> String.split(~r{}, trim: true) | |
| |> Enum.reduce 0, fn(x, acc) -> @scrabble_score[String.to_atom x] + acc end | |
| end | |
| # Old way I was experiencing issues with before | |
| def old_score(word) do | |
| word | |
| |> String.strip | |
| |> String.downcase | |
| |> String.split(~r{}, trim: true) | |
| |> Enum.map &(@scrabble_score[String.to_atom &1]) | |
| |> IO.inspect | |
| |> Enum.reduce fn(x, acc) -> x + acc end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment