Skip to content

Commit 6f608ad

Browse files
authored
Add solution for Challenge 6 by WHFF521 (#1890)
Auto-merged after 2 days with all checks passing. PR: #1890 Author: @WHFF521
1 parent abfdcd5 commit 6f608ad

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Package challenge6 contains the solution for Challenge 6.
2+
package challenge6
3+
4+
import (
5+
// Add any necessary imports here
6+
"regexp"
7+
"strings"
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+
var wordRegex = regexp.MustCompile(`[a-zA-Z0-9]+(?:'[a-zA-Z0-9]+)*`)
22+
func CountWordFrequency(text string) map[string]int {
23+
// Your implementation here
24+
words := wordRegex.FindAllString(text,-1)
25+
frequencyMap := make(map[string]int)
26+
27+
for _,word := range(words){
28+
lowerWord := strings.ToLower(word)
29+
lowerWord = strings.ReplaceAll(lowerWord,"'","")
30+
frequencyMap[lowerWord]++
31+
}
32+
return frequencyMap
33+
}

0 commit comments

Comments
 (0)