QAYENICNXLIWC3QFMWQU6ILAL3WGRUBTW6SND737Y7DKLV5ZTNIAC
package scrabble
import "testing"
func TestScore(t *testing.T) {
for _, test := range scrabbleScoreTests {
if actual := Score(test.input, valueEnglish); 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, valueEnglish)
}
}
}
func TestHunScore(t *testing.T) {
for _, test := range scrabbleHunScoreTests {
if actual := Score(test.input, valueHungarian, valueHungarianString); actual != test.expected {
t.Errorf("Score(%q) expected %d, Actual %d", test.input, test.expected, actual)
}
}
}
func BenchmarkHunScore(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, test := range scrabbleHunScoreTests {
Score(test.input, valueHungarian, valueHungarianString)
}
}
}
/*
Package scrabble is for handling Scrabble related tasks
*/
package scrabble
import (
"strings"
)
// Score calculates Scrabble score for the input string
func Score(input string, value map[rune]int, strvalue ...map[string]int) (score int) {
input = strings.ToUpper(input)
if strvalue != nil {
for double, point := range strvalue[0] {
score += strings.Count(input, double) * point
input = strings.ReplaceAll(input, double, "")
}
}
for _, v := range input {
score += value[v]
}
return score
}
package scrabble
var valueEnglish = 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,
}
var valueHungarian = map[rune]int{
'A': 1, 'E': 1, 'K': 1, 'T': 1, 'Á': 1, 'L': 1, 'N': 1, 'R': 1, 'I': 1, 'M': 1, 'O': 1, 'S': 1, 'B': 2, 'D': 2, 'G': 2, 'Ó': 2, 'É': 3, 'H': 3, 'V': 3, 'F': 4, 'J': 4, 'Ö': 4, 'P': 4, 'U': 4, 'Ü': 4, 'Z': 4, 'C': 5, 'Í': 5, 'Ő': 7, 'Ú': 7, 'Ű': 7,
}
var valueHungarianString = map[string]int{
"SZ": 3, "GY": 4, "NY": 5, "CS": 7, "LY": 8, "ZS": 8, "TY": 10,
}
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
}
var scrabbleHunScoreTests = []scrabbleTest{
{"á", 1}, // lowercase letter
{"Á", 1}, // uppercase letter
{"í", 5}, // valuable letter
{"ég", 3 + 2}, // short word
{"űri", 7 + 1 + 1}, // short, valuable word
{"dobogó", 2 + 1 + 2 + 1 + 2 + 2}, // medium word
{"űrhajó", 7 + 1 + 3 + 1 + 4 + 2}, // medium, valuable word
{"Húsrágó-Hídverő", 3 + 7 + 1 + 1 + 1 + 2 + 2 + 0 + 3 + 5 + 2 + 3 + 1 + 1 + 7}, // long, mixed-case word
{"", 0}, // empty input
{"Tyúkól", 10 + 7 + 1 + 2 + 1}, // double letter, mixed case
{"LYukacsos", 8 + 4 + 1 + 1 + 7 + 1 + 1}, // multiple double letters, uper or lower case
{"aábcdeéfghiíjklmnoóöőprstuúüűvz", 12*1 + 4*2 + 3*3 + 7*4 + 2*5 + 3*7}, // entire alphabet
{"aábccsdeéfggyhiíjkllymnnyoóöőprsszttyuúüűvzzs", 12*1 + 4*2 + 3*3 + 7*4 + 2*5 + 3*7 + 3 + 4 + 5 + 7 + 8 + 8 + 10}, // entire alphabet with doubles
}