From d40949d4cfc17d784980f3bbc5da71b339539f48 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Sun, 28 Aug 2016 01:47:28 +0200 Subject: Solve wc --- go/word-count/word_count.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 go/word-count/word_count.go (limited to 'go/word-count/word_count.go') diff --git a/go/word-count/word_count.go b/go/word-count/word_count.go new file mode 100644 index 0000000..9ced5a7 --- /dev/null +++ b/go/word-count/word_count.go @@ -0,0 +1,30 @@ +package wordcount + +import ( + "strings" + "unicode" +) + +const testVersion = 2 + +// Use this return type. +type Frequency map[string]int + +func normalize(s string) string { + var ret []rune + for _, r := range s { + if unicode.In(r, unicode.Letter, unicode.Number, unicode.Space) { + ret = append(ret, unicode.ToLower(r)) + } + } + return string(ret) +} + +// Just implement the function. +func WordCount(phrase string) Frequency { + wc := make(Frequency) + for _, v := range strings.Fields(normalize(phrase)) { + wc[v]++ + } + return wc +} -- cgit v1.2.3