The dig deeper section on the exercises are a great way checking and understanding different approaches to the problems.
On the Scrabble exercise, the dig deeper solutions go over two possible ways to solve it, using a map, or using switch case, with the switch case being the faster one.
I was wondering if there was any place for the array of ints solution, that should be even faster, and still quite ‘vanilla’, at least on C/CPP. Is it not considered idiomatic or good practice in Go, and therefore not recommended?
Here is a quick example of what I mean. The tmpLetter is superfluous on the code below, and we can add to the score directly, making it even a bit faster.
package scrabble
// pointsTable stores the points for each letter. The first position is the points for the letter a,
// the second for letter b, and so forth.
var pointsTable = [26]int{
// A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z
1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10}
// Score returns the score of a given word in scrabble.
func Score(word string) int {
var score int
var tmpLetter byte
for i := 0; i < len(word); i++ {
if word[i] > 'Z' {
tmpLetter = word[i] - 'a'
} else {
tmpLetter = word[i] - 'A'
}
score += pointsTable[tmpLetter]
}
return score
}