summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2016-08-28 01:47:28 +0200
committerDimitri Sokolyuk <demon@dim13.org>2016-08-28 01:47:28 +0200
commitd40949d4cfc17d784980f3bbc5da71b339539f48 (patch)
treeea9a09192a0403d35308dae55e61eb7de130c030
parentc2cab3dd8e3023a524f6ad3262790bfce2fda2c0 (diff)
Solve wc
-rw-r--r--go/word-count/README.md31
-rw-r--r--go/word-count/cases_test.go41
-rw-r--r--go/word-count/word_count.go30
-rw-r--r--go/word-count/word_count_test.go31
4 files changed, 133 insertions, 0 deletions
diff --git a/go/word-count/README.md b/go/word-count/README.md
new file mode 100644
index 0000000..1f11b58
--- /dev/null
+++ b/go/word-count/README.md
@@ -0,0 +1,31 @@
+# Word Count
+
+Write a program that given a phrase can count the occurrences of each word in that phrase.
+
+For example for the input `"olly olly in come free"`
+
+```plain
+olly: 2
+in: 1
+come: 1
+free: 1
+```
+
+
+To run the tests simply run the command `go test` in the exercise directory.
+
+If the test suite contains benchmarks, you can run these with the `-bench`
+flag:
+
+ go test -bench .
+
+For more detailed info about the Go track see the [help
+page](http://exercism.io/languages/go).
+
+## Source
+
+This is a classic toy problem, but we were reminded of it by seeing it in the Go Tour.
+
+## Submitting Incomplete Problems
+It's possible to submit an incomplete solution so you can see how others have completed the exercise.
+
diff --git a/go/word-count/cases_test.go b/go/word-count/cases_test.go
new file mode 100644
index 0000000..204e122
--- /dev/null
+++ b/go/word-count/cases_test.go
@@ -0,0 +1,41 @@
+package wordcount
+
+// Source: exercism/x-common
+// Commit: 3b07e53 Merge pull request #117 from mikeyjcat/add-raindrops-json
+
+var testCases = []struct {
+ description string
+ input string
+ output Frequency
+}{
+ {
+ "count one word",
+ "word",
+ Frequency{"word": 1},
+ },
+ {
+ "count one of each word",
+ "one of each",
+ Frequency{"each": 1, "of": 1, "one": 1},
+ },
+ {
+ "multiple occurrences of a word",
+ "one fish two fish red fish blue fish",
+ Frequency{"blue": 1, "fish": 4, "one": 1, "red": 1, "two": 1},
+ },
+ {
+ "ignore punctuation",
+ "car : carpet as java : javascript!!&@$%^&",
+ Frequency{"as": 1, "car": 1, "carpet": 1, "java": 1, "javascript": 1},
+ },
+ {
+ "include numbers",
+ "testing, 1, 2 testing",
+ Frequency{"1": 1, "2": 1, "testing": 2},
+ },
+ {
+ "normalize case",
+ "go Go GO",
+ Frequency{"go": 3},
+ },
+}
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
+}
diff --git a/go/word-count/word_count_test.go b/go/word-count/word_count_test.go
new file mode 100644
index 0000000..98cf6b0
--- /dev/null
+++ b/go/word-count/word_count_test.go
@@ -0,0 +1,31 @@
+package wordcount
+
+import (
+ "reflect"
+ "testing"
+)
+
+const targetTestVersion = 2
+
+func TestWordCount(t *testing.T) {
+ if testVersion != targetTestVersion {
+ t.Fatalf("Found testVersion = %v, want %v", testVersion, targetTestVersion)
+ }
+ for _, tt := range testCases {
+ expected := tt.output
+ actual := WordCount(tt.input)
+ if !reflect.DeepEqual(actual, expected) {
+ t.Fatalf("%s\n\tExpected: %v\n\tGot: %v", tt.description, expected, actual)
+ } else {
+ t.Logf("PASS: %s - WordCount(%s)", tt.description, tt.input)
+ }
+ }
+}
+
+func BenchmarkWordCount(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ for _, tt := range testCases {
+ WordCount(tt.input)
+ }
+ }
+}