Monday, November 30, 2015

Exercise: Maps 第一段 Go 语言测试通过了!

打算转换职业方向,做Golang程序设计师。
第一段代码测试通过了,好激动!

My code:

package main
import (
 "golang.org/x/tour/wc"
 "strings"
)

func WordCount(s string) map[string]int {
 var la_s = strings.Split(s, " ")

 //lm_s := map[string]int{}  // simpler
 var lm_s map[string]int
 lm_s = make(map[string]int)
 for _, word := range la_s {
  lm_s[word] = lm_s[word] + 1
 }
 return lm_s
 //return map[string]int{"x": 1}
}

func main() {
 wc.Test(WordCount)
}


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

Program exited.
Reference:

No comments: