Avatar of paulfioravanti

paulfioravanti's solution

to Scrabble Score in the Elm Track

Published at Jun 25 2019 · 0 comments
Instructions
Test suite
Solution

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.

Elm Installation

Refer to the Installing Elm page for information about installing elm.

Writing the Code

The first time you start an exercise, you'll need to ensure you have the appropriate dependencies installed. Thankfully, Elm makes that easy for you and will install dependencies when you try to run tests or build the code.

Execute the tests with:

$ elm-test

Automatically run tests again when you save changes:

$ elm-test --watch

As you work your way through the test suite, be sure to remove the skip <| calls from each test until you get them all passing!

Source

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

Submitting Incomplete Solutions

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

Tests.elm

module Tests exposing (tests)

import Expect
import ScrabbleScore exposing (scoreWord)
import Test exposing (..)


tests : Test
tests =
    describe "ScrabbleScore"
        [ test "lowercase letter" <|
            \() -> Expect.equal 1 (scoreWord "a")
        , skip <|
            test "uppercase letter" <|
                \() -> Expect.equal 1 (scoreWord "A")
        , skip <|
            test "valuable letter" <|
                \() -> Expect.equal 4 (scoreWord "f")
        , skip <|
            test "short word" <|
                \() -> Expect.equal 2 (scoreWord "at")
        , skip <|
            test "short, valuable word" <|
                \() -> Expect.equal 12 (scoreWord "zoo")
        , skip <|
            test "medium word" <|
                \() -> Expect.equal 6 (scoreWord "street")
        , skip <|
            test "medium, valuable word" <|
                \() -> Expect.equal 22 (scoreWord "quirky")
        , skip <|
            test "long, mixed-case word" <|
                \() -> Expect.equal 41 (scoreWord "OxyphenButazone")
        , skip <|
            test "english-like word" <|
                \() -> Expect.equal 8 (scoreWord "pinata")
        , skip <|
            test "non-english letter is not scored" <|
                \() -> Expect.equal 7 (scoreWord "piñata")
        , skip <|
            test "empty input" <|
                \() -> Expect.equal 0 (scoreWord "")
        ]
module ScrabbleScore exposing (scoreWord)


scoreWord : String -> Int
scoreWord x =
    x
        |> String.toLower
        |> String.toList
        |> List.foldl addScoreForLetter 0



-- PRIVATE


addScoreForLetter : Char -> Int -> Int
addScoreForLetter char acc =
    if onePoint char then
        acc + 1

    else if twoPoints char then
        acc + 2

    else if threePoints char then
        acc + 3

    else if fourPoints char then
        acc + 4

    else if fivePoints char then
        acc + 5

    else if eightPoints char then
        acc + 8

    else if tenPoints char then
        acc + 10

    else
        acc


onePoint : Char -> Bool
onePoint char =
    let
        onePointCharacters =
            [ 'a', 'e', 'i', 'o', 'u', 'l', 'n', 'r', 's', 't' ]
    in
    List.member char onePointCharacters


twoPoints : Char -> Bool
twoPoints char =
    let
        twoPointCharacters =
            [ 'd', 'g' ]
    in
    List.member char twoPointCharacters


threePoints : Char -> Bool
threePoints char =
    let
        threePointCharacters =
            [ 'b', 'c', 'm', 'p' ]
    in
    List.member char threePointCharacters


fourPoints : Char -> Bool
fourPoints char =
    let
        fourPointCharacters =
            [ 'f', 'h', 'v', 'w', 'y' ]
    in
    List.member char fourPointCharacters


fivePoints : Char -> Bool
fivePoints char =
    char == 'k'


eightPoints : Char -> Bool
eightPoints char =
    let
        eightPointCharacters =
            [ 'j', 'x' ]
    in
    List.member char eightPointCharacters


tenPoints : Char -> Bool
tenPoints char =
    let
        tenPointCharacters =
            [ 'q', 'z' ]
    in
    List.member char tenPointCharacters

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

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.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?