Created
February 6, 2019 09:42
-
-
Save zerogvt/d9fd7fd05c0094b8841e4ae76b341b79 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Exercise: Maps | |
Implement WordCount. It should return a map of the counts of each “word” in the string s. The wc.Test function runs a test suite against the provided function and prints success or failure. | |
You might find strings.Fields helpful. | |
https://tour.golang.org/moretypes/23 | |
*/ | |
package main | |
import ( | |
"strings" | |
"golang.org/x/tour/wc" | |
) | |
func WordCount(s string) map[string]int { | |
m := make(map[string]int) | |
for _, w := range strings.Fields(s) { | |
m[w]++ | |
} | |
return m | |
} | |
func main() { | |
wc.Test(WordCount) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment