Skip to content

Commit 7976a11

Browse files
committed
Add solution for Challenge 6 by yz4230
1 parent 4c1764d commit 7976a11

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Package challenge6 contains the solution for Challenge 6.
2+
package challenge6
3+
4+
import "strings"
5+
6+
func isSpace(c rune) bool {
7+
return c == ' ' || c == '\t' || c == '\n' || c == '-'
8+
}
9+
10+
func isLetter(c rune) bool {
11+
return ('0' <= c && c <= '9') || ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z')
12+
}
13+
14+
func CountWordFrequency(text string) map[string]int {
15+
// Your implementation here
16+
m := make(map[string]int)
17+
parts := make([]string, 0)
18+
19+
buf := ""
20+
for _, c := range text {
21+
if len(buf) > 0 && isSpace(c) {
22+
parts = append(parts, buf)
23+
buf = ""
24+
} else if isLetter(c) {
25+
buf += string(c)
26+
}
27+
}
28+
if len(buf) > 0 {
29+
parts = append(parts, buf)
30+
}
31+
32+
for _, part := range parts {
33+
m[strings.ToLower(part)]++
34+
}
35+
36+
return m
37+
}

0 commit comments

Comments
 (0)