Skip to content

Instantly share code, notes, and snippets.

@zh-f
Last active June 3, 2020 09:07
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 zh-f/53dfe574ad9f10bbdd8c85471a8c87d8 to your computer and use it in GitHub Desktop.
Save zh-f/53dfe574ad9f10bbdd8c85471a8c87d8 to your computer and use it in GitHub Desktop.
Exercise Maps Solution
package main
import (
"golang.org/x/tour/wc"
"strings"
)
func WordCount(s string) map[string]int {
counter := make(map[string]int)
for _, v := range strings.Fields(s) {
counter[v] += 1
}
return counter
}
func main() {
wc.Test(WordCount)
}
@zh-f
Copy link
Author

zh-f commented Jun 3, 2020

Result

PASS
 f("I am learning Go!") = 
  map[string]int{"Go!":1, "I":1, "am":1, "learning":1}
PASS
 f("The quick brown fox jumped over the lazy dog.") = 
  map[string]int{"The":1, "brown":1, "dog.":1, "fox":1, "jumped":1, "lazy":1, "over":1, "quick":1, "the":1}
PASS
 f("I ate a donut. Then I ate another donut.") = 
  map[string]int{"I":2, "Then":1, "a":1, "another":1, "ate":2, "donut.":2}
PASS
 f("A man a plan a canal panama.") = 
  map[string]int{"A":1, "a":2, "canal":1, "man":1, "panama.":1, "plan":1}

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