Skip to content

Commit b766b6e

Browse files
authored
Add solution for Challenge 6 by Raycas96 (#1566)
Auto-merged after 2 days with all checks passing. PR: #1566 Author: @Raycas96
1 parent 35e6659 commit b766b6e

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

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+
"strings"
6+
"unicode"
7+
)
8+
9+
// CountWordFrequency takes a string containing multiple words and returns
10+
// a map where each key is a word and the value is the number of times that
11+
// word appears in the string. The comparison is case-insensitive.
12+
//
13+
// Words are defined as sequences of letters and digits.
14+
// All words are converted to lowercase before counting.
15+
// All punctuation, spaces, and other non-alphanumeric characters are ignored.
16+
//
17+
// For example:
18+
// Input: "The quick brown fox jumps over the lazy dog."
19+
// Output: map[string]int{"the": 2, "quick": 1, "brown": 1, "fox": 1, "jumps": 1, "over": 1, "lazy": 1, "dog": 1}
20+
func CountWordFrequency(text string) map[string]int {
21+
counts := make(map[string]int)
22+
23+
text = strings.ToLower(text)
24+
text = strings.ReplaceAll(text, "'", "")
25+
words := strings.FieldsFunc(text, func(r rune) bool {
26+
return !unicode.IsLetter(r) && !unicode.IsDigit(r)
27+
})
28+
29+
for _, word := range words {
30+
counts[word]++
31+
}
32+
33+
return counts
34+
}

0 commit comments

Comments
 (0)