Drill

ProblemsGo › text

Count the words that matter

mediumtextHash mapsStringsParsingGo

A support tool skims ticket text to see which terms keep coming up, ignoring the little joining words.

wordFrequency(text: string) → map<string, int>

Go needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.

Solve it in Python →

Where you start

func wordFrequency(text string) map[string]int {
	
}

Worked examples

CallResult
wordFrequency("card declined card")map[string]int{"card": 2, "declined": 1}
wordFrequency("The the THE")map[string]int{"the": 3}
wordFrequency("a an I ok")map[string]int{}
wordFrequency("")map[string]int{}

Hint

Lowercase before counting, or "Payment" and "payment" end up as two entries.

Reference solution in Go
func wordFrequency(text string) map[string]int {
	result := map[string]int{}
	for _, w := range strings.Fields(strings.ToLower(text)) {
		if len(w) < 3 {
			continue
		}
		result[w]++
	}
	return result
}

The same problem in another language

More text problems in Go