Created
December 2, 2018 01:09
-
-
Save bilsalak/51d0f26bb9bb02f307b8583cabcfef28 to your computer and use it in GitHub Desktop.
A Tour of Go - Exercise: Maps
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
// 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. | |
package main | |
import ( | |
"strings" | |
"golang.org/x/tour/wc" | |
) | |
func WordCount(s string) map[string]int { | |
word_count_map := make(map[string]int) | |
words := strings.Fields(s) | |
for _, word := range words { | |
element, _ := word_count_map[word] | |
word_count := element + 1 | |
word_count_map[word] = word_count | |
} | |
return word_count_map | |
} | |
func main() { | |
wc.Test(WordCount) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment