Skip to content

Commit 358ca74

Browse files
Add solution for Challenge 6 (#1187)
Co-authored-by: go-interview-practice-bot[bot] <230190823+go-interview-practice-bot[bot]@users.noreply.github.com>
1 parent 1bda82e commit 358ca74

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Package challenge6 contains the solution for Challenge 6.
2+
package challenge6
3+
4+
import (
5+
// Add any necessary imports here
6+
"strings"
7+
"unicode"
8+
)
9+
10+
// CountWordFrequency takes a string containing multiple words and returns
11+
// a map where each key is a word and the value is the number of times that
12+
// word appears in the string. The comparison is case-insensitive.
13+
//
14+
// Words are defined as sequences of letters and digits.
15+
// All words are converted to lowercase before counting.
16+
// All punctuation, spaces, and other non-alphanumeric characters are ignored.
17+
//
18+
// For example:
19+
// Input: "The quick brown fox jumps over the lazy dog."
20+
// Output: map[string]int{"the": 2, "quick": 1, "brown": 1, "fox": 1, "jumps": 1, "over": 1, "lazy": 1, "dog": 1}
21+
func CountWordFrequency(text string) map[string]int {
22+
// Your implementation here
23+
res := make(map[string]int)
24+
newText := strings.Replace(text, "'", "", -1)
25+
words := strings.FieldsFunc(newText, func(c rune) bool {
26+
return !unicode.IsLetter(c) && !unicode.IsNumber(c)
27+
})
28+
29+
for _, word := range words {
30+
res[strings.ToLower(word)]++
31+
}
32+
33+
return res
34+
}

0 commit comments

Comments
 (0)