Created
May 10, 2014 04:32
-
-
Save pyk/5234d820218940561b5e to your computer and use it in GitHub Desktop.
Maps exercise solution http://tour.golang.org/#43
This file contains hidden or 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
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{"brown":1, "over":1, "lazy":1, "dog.":1, "The":1, "quick":1, "fox":1, "jumped":1, "the":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. |
This file contains hidden or 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
package main | |
import ( | |
"strings" | |
"code.google.com/p/go-tour/wc" | |
) | |
func WordCount(s string) map[string]int { | |
var x = make(map[string]int) | |
for _,v := range strings.Fields(s) { | |
x[v]++ | |
} | |
return x | |
} | |
func main() { | |
wc.Test(WordCount) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment