šŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io šŸŽ‰
Avatar of SergiiVlasiuk

SergiiVlasiuk's solution

to Scrabble Score in the Scala Track

Published at Aug 23 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.

The Scala exercises assume an SBT project scheme. The exercise solution source should be placed within the exercise directory/src/main/scala. The exercise unit tests can be found within the exercise directory/src/test/scala.

To run the tests simply run the command sbt test in the exercise directory.

For more detailed info about the Scala track see the help page.

Source

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

Submitting Incomplete Solutions

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

ScrabbleScoreTest.scala

import org.scalatest.{Matchers, FunSuite}

/** @version 1.1.0 */
class ScrabbleScoreTest extends FunSuite with Matchers {

  test("lowercase letter") {
    ScrabbleScore.score("a") should be(1)
  }

  test("uppercase letter") {
    pending
    ScrabbleScore.score("A") should be(1)
  }

  test("valuable letter") {
    pending
    ScrabbleScore.score("f") should be(4)
  }

  test("short word") {
    pending
    ScrabbleScore.score("at") should be(2)
  }

  test("short, valuable word") {
    pending
    ScrabbleScore.score("zoo") should be(12)
  }

  test("medium word") {
    pending
    ScrabbleScore.score("street") should be(6)
  }

  test("medium, valuable word") {
    pending
    ScrabbleScore.score("quirky") should be(22)
  }

  test("long, mixed-case word") {
    pending
    ScrabbleScore.score("OxyphenButazone") should be(41)
  }

  test("english-like word") {
    pending
    ScrabbleScore.score("pinata") should be(8)
  }

  test("empty input") {
    pending
    ScrabbleScore.score("") should be(0)
  }

  test("entire alphabet available") {
    pending
    ScrabbleScore.score("abcdefghijklmnopqrstuvwxyz") should be(87)
  }
}
object ScrabbleScore {
  def scoreLetter: (Char) => Int = _ match {
    case 'A' | 'E' | 'I' | 'O' | 'U' | 'L' | 'N' | 'R' | 'S' | 'T' => 1
    case 'D' | 'G' => 2
    case 'B' | 'C' | 'M' | 'P' => 3
    case 'F' | 'H' | 'V' | 'W' | 'Y' => 4
    case 'K' => 5
    case 'J' | 'X' => 8
    case 'Q' | 'Z' => 10
    case _ => 0
  }

  def score(word: String) = word.toUpperCase.map(scoreLetter).sum
}

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?