-
Notifications
You must be signed in to change notification settings - Fork 0
/
highestScoringWords.go
60 lines (44 loc) · 1.08 KB
/
highestScoringWords.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package kata
import (
"fmt"
"strings"
)
func High(s string) string {
// your code here
highestScore := 0
highestScoredWord := ""
// split string into words
words := strings.Split(s, " ")
// loop through words
for _, word := range words {
// get score for each word
score := getScore(word)
// fmt.Println(word, "---->", score)
// if score is higher than highestScore, set highestScore to score
if score > highestScore {
highestScore = score
highestScoredWord = word
}
}
return highestScoredWord
}
func getScore(word string) int {
// create a map of letters and their scores
letterScores := map[string]int{}
// loop through alphabet and assign scores to letters
for i := 97; i <= 122; i++ {
// set the letter to the string representation of i
letter := fmt.Sprintf("%c", i)
// convert i to string and assign to map
letterScores[letter] = i - 96
}
// set score to 0
score := 0
// loop through word
for _, letter := range word {
// add score for each letter to score
score += letterScores[string(letter)]
}
// return score
return score
}