# artemkorsakov's solution

## to Word Count in the Go Track

Published at Feb 21 2019 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution was written.

Given a phrase, count the occurrences of each word in that phrase.

For example for the input `"olly olly in come free"`

``````olly: 2
in: 1
come: 1
free: 1
``````

## Running the tests

To run the tests run the command `go test` from within the exercise directory.

If the test suite contains benchmarks, you can run these with the `--bench` and `--benchmem` flags:

``````go test -v --bench . --benchmem
``````

Keep in mind that each reviewer will run benchmarks on a different machine, with different specs, so the results from these benchmark tests may vary.

## Further information

For more detailed information about the Go track, including how to get help if you're having trouble, please visit the exercism.io Go language page.

## Source

This is a classic toy problem, but we were reminded of it by seeing it in the Go Tour.

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### cases_test.go

``````package wordcount

// Source: exercism/problem-specifications
// Commit: 77623ec word-count: Use camel-case for property name word-count: remove newline from eof
// Problem Specifications Version: 1.2.0

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},
},
{
"handles cramped lists",
"one,two,three",
Frequency{"one": 1, "three": 1, "two": 1},
},
{
"handles expanded lists",
"one,\ntwo,\nthree",
Frequency{"one": 1, "three": 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 Stop stop",
Frequency{"go": 3, "stop": 2},
},
{
"with apostrophes",
"First: don't laugh. Then: don't cry.",
Frequency{"cry": 1, "don't": 2, "first": 1, "laugh": 1, "then": 1},
},
{
"with quotations",
"Joe can't tell between 'large' and large.",
Frequency{"and": 1, "between": 1, "can't": 1, "joe": 1, "large": 2, "tell": 1},
},
{
"multiple spaces not detected as a word",
" multiple   whitespaces",
Frequency{"multiple": 1, "whitespaces": 1},
},
}``````

### word_count_test.go

``````package wordcount

import (
"reflect"
"testing"
)

// wordcount API
//
// func WordCount(phrase string) Frequency  // Implement this function.
// type Frequency map[string]int            // Using this return type.

func TestWordCount(t *testing.T) {
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)
}
}
}``````
``````package wordcount

import (
"regexp"
"strings"
)

var reg = regexp.MustCompile(`([1-9]+|[a-z]+'?[a-z]+)`)

// Frequency is the map where the key is the word and the value is the number of the occurrences.
type Frequency map[string]int

// WordCount counts the occurrences of each word in that phrase.
func WordCount(input string) Frequency {
result := make(map[string]int)
words := reg.FindAllString(strings.ToLower(input), -1)
for _, word := range words {
result[word] = result[word] + 1
}
return result
}``````