Skip to content

Instantly share code, notes, and snippets.

@takatoshiono
Created August 30, 2016 22:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takatoshiono/354a541a5a8edf44adf486290bfff7fb to your computer and use it in GitHub Desktop.
Save takatoshiono/354a541a5a8edf44adf486290bfff7fb to your computer and use it in GitHub Desktop.
A Tour of Go: Exercise Maps, https://go-tour-jp.appspot.com/moretypes/23
package main
import (
"golang.org/x/tour/wc"
"strings"
)
func WordCount(s string) map[string]int {
words := strings.Fields(s)
wordsCount := make(map[string]int)
for i := 0; i < len(words); i++ {
wordsCount[words[i]]++
}
return wordsCount
}
func main() {
wc.Test(WordCount)
}
@takatoshiono
Copy link
Author

Result

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{"quick":1, "fox":1, "jumped":1, "over":1, "lazy":1, "The":1, "the":1, "dog.":1, "brown":1}
PASS
 f("I ate a donut. Then I ate another donut.") = 
  map[string]int{"another":1, "I":2, "ate":2, "a":1, "donut.":2, "Then":1}
PASS
 f("A man a plan a canal panama.") = 
  map[string]int{"panama.":1, "A":1, "man":1, "a":2, "plan":1, "canal":1}

Program exited.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment