利用 hash table 实现单词统计
28 Sep 2017package main
import (
"golang.org/x/tour/wc"
"strings"
)
func WordCount(s string) map[string]int {
wordcount := make(map[string]int)
words := strings.Fields(s)
for _, w := range words {
wordcount[w] += 1
}
return wordcount
}
func main() {
wc.Test(WordCount)
}
PASS
f("I am learning Go!") =
map[string]int{"I":1, "am":1, "learning":1, "Go!":1}
PASS
f("The quick brown fox jumped over the lazy dog.") =
map[string]int{"The":1, "quick":1, "brown":1, "jumped":1, "lazy":1, "fox":1, "over":1, "the":1, "dog.":1}
PASS
f("I ate a donut. Then I ate another donut.") =
map[string]int{"Then":1, "another":1, "I":2, "ate":2, "a":1, "donut.":2}
PASS
f("A man a plan a canal panama.") =
map[string]int{"A":1, "man":1, "a":2, "plan":1, "canal":1, "panama.":1}