-
Notifications
You must be signed in to change notification settings - Fork 0
/
maxwordcountn.go
51 lines (45 loc) · 1.38 KB
/
maxwordcountn.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
package piscine
import (
"sort"
)
// MaxWordCountN returns a map of the n most frequently occurring words in the input string 'text',
// along with their respective counts.
func MaxWordCountN(text string, n int) map[string]int {
// Split 'text' into individual words using spaces as delimiters
words := make([]string, 0)
start := 0
for i, r := range text {
if r == ' ' {
words = append(words, text[start:i])
start = i + 1
}
}
words = append(words, text[start:])
// Count the occurrences of each word in 'words'
wordCount := make(map[string]int)
for _, word := range words {
wordCount[word]++
}
// Sort the words in descending order of frequency and ascending order of word (for ties)
sortedWords := make([]string, 0, len(wordCount))
for word := range wordCount {
sortedWords = append(sortedWords, word)
}
sort.Slice(sortedWords, func(i, j int) bool {
if wordCount[sortedWords[i]] != wordCount[sortedWords[j]] {
return wordCount[sortedWords[i]] > wordCount[sortedWords[j]]
}
return sortedWords[i] < sortedWords[j]
})
// Restrict the results to the top 'n' words (or less if there are ties)
if n > len(sortedWords) {
n = len(sortedWords)
}
tiedWords := sortedWords[:n]
// Construct a new map containing the top 'n' words and their counts
result := make(map[string]int)
for _, word := range tiedWords {
result[word] = wordCount[word]
}
return result
}