Initial Commit
This commit is contained in:
27
go/word-count/README.md
Normal file
27
go/word-count/README.md
Normal file
@@ -0,0 +1,27 @@
|
||||
# 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. [view source]()
|
41
go/word-count/cases_test.go
Normal file
41
go/word-count/cases_test.go
Normal file
@@ -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},
|
||||
},
|
||||
}
|
28
go/word-count/word_count.go
Normal file
28
go/word-count/word_count.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package wordcount
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// TestVersion is an exercism thing
|
||||
const TestVersion = 1
|
||||
|
||||
// Frequency is the return type
|
||||
type Frequency map[string]int
|
||||
|
||||
// WordCount returns how many of each word in 'phrase'
|
||||
func WordCount(phrase string) Frequency {
|
||||
ret := make(Frequency)
|
||||
words := strings.Split(phrase, " ")
|
||||
cleanUp, _ := regexp.Compile("[^A-Za-z0-9]")
|
||||
for _, v := range words {
|
||||
// The test cases assume that we're stripping non-alphanumeric runes
|
||||
// and making it lower case
|
||||
v = strings.ToLower(cleanUp.ReplaceAllString(v, ""))
|
||||
if v != "" {
|
||||
ret[v]++
|
||||
}
|
||||
}
|
||||
return ret
|
||||
}
|
34
go/word-count/word_count_test.go
Normal file
34
go/word-count/word_count_test.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package wordcount
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
const testVersion = 1
|
||||
|
||||
// Retired testVersions
|
||||
// (none) 133d626683b18a22f9aa59ad0990c7d0f565291c
|
||||
|
||||
func TestWordCount(t *testing.T) {
|
||||
if TestVersion != testVersion {
|
||||
t.Fatalf("Found TestVersion = %v, want %v", TestVersion, testVersion)
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user