Skip to content

Instantly share code, notes, and snippets.

@sin2akshay
Last active May 24, 2018 11:46
Show Gist options
  • Save sin2akshay/c2db1e65ce7e40151111be4f84ca8232 to your computer and use it in GitHub Desktop.
Save sin2akshay/c2db1e65ce7e40151111be4f84ca8232 to your computer and use it in GitHub Desktop.
Tour of Golang | Exercise: Maps - https://tour.golang.org/moretypes/23
package main
import (
"golang.org/x/tour/wc"
"strings"
)
func WordCount(s string) map[string]int {
a := strings.Fields(s)
m := make(map[string]int)
for _, element := range a {
m[element] += 1
}
return m
}
func main() {
wc.Test(WordCount)
}
@sin2akshay
Copy link
Author

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

Program exited.

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