Exercism v3 launches on Sept 1st 2021. Learn more! ๐๐๐

rootulp's solution

to Scrabble Score in the Go Track

Published at Jun 02 2020 · 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.

Coding the solution

Look for a stub file having the name scrabble_score.go and place your solution code in that file.

Running the tests

To run the tests run the command `go test` from within the exercise directory.

If the test suite contains benchmarks, you can run these with the `--bench` and `--benchmem` flags:

``````go test -v --bench . --benchmem
``````

Keep in mind that each reviewer will run benchmarks on a different machine, with different specs, so the results from these benchmark tests may vary.

Further information

For more detailed information about the Go track, including how to get help if you're having trouble, please visit the exercism.io Go language 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.

cases_test.go

``````package scrabble

// Source: exercism/problem-specifications
// Commit: 0d882ed scrabble-score: Apply new "input" policy
// Problem Specifications Version: 1.1.0

type scrabbleTest struct {
input    string
expected int
}

var scrabbleScoreTests = []scrabbleTest{
{"a", 1},                           // lowercase letter
{"A", 1},                           // uppercase letter
{"f", 4},                           // valuable letter
{"at", 2},                          // short word
{"zoo", 12},                        // short, valuable word
{"street", 6},                      // medium word
{"quirky", 22},                     // medium, valuable word
{"OxyphenButazone", 41},            // long, mixed-case word
{"pinata", 8},                      // english-like word
{"", 0},                            // empty input
{"abcdefghijklmnopqrstuvwxyz", 87}, // entire alphabet available
}``````

scrabble_score_test.go

``````package scrabble

import "testing"

func TestScore(t *testing.T) {
for _, test := range scrabbleScoreTests {
if actual := Score(test.input); actual != test.expected {
t.Errorf("Score(%q) expected %d, Actual %d", test.input, test.expected, actual)
}
}
}

func BenchmarkScore(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, test := range scrabbleScoreTests {
Score(test.input)
}
}
}``````
``````package scrabble

import "unicode"

var scrabbleLetterValues = map[rune]int{
'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,
}

// Score returns the Scrabble score for the provided word.
func Score(word string) int {
score := 0
for _, c := range word {
score += scrabbleLetterValues[unicode.ToUpper(c)]
}
return score
}``````

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?