美文网首页
【A Tour of Go 练习】:映射

【A Tour of Go 练习】:映射

作者: 马蹄哒 | 来源:发表于2020-02-16 13:00 被阅读0次

实现 WordCount。它应当返回一个映射,其中包含字符串 s 中每个“单词”的个数。函数 wc.Test 会对此函数执行一系列测试用例,并输出成功还是失败。

你会发现 strings.Fields 很有帮助。

解答:

package main

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

func WordCount(s string) map[string]int {
    words := strings.Fields(s)
    result := make(map[string]int)
    for _, w := range(words){
        result[w] += 1
    }
    return result
}

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

结果

PASS
 f("I am learning Go!") = 
  map[string]int{"Go!":1, "I":1, "am":1, "learning":1}
PASS
 f("The quick brown fox jumped over the lazy dog.") = 
  map[string]int{"The":1, "brown":1, "dog.":1, "fox":1, "jumped":1, "lazy":1, "over":1, "quick":1, "the":1}
PASS
 f("I ate a donut. Then I ate another donut.") = 
  map[string]int{"I":2, "Then":1, "a":1, "another":1, "ate":2, "donut.":2}
PASS
 f("A man a plan a canal panama.") = 
  map[string]int{"A":1, "a":2, "canal":1, "man":1, "panama.":1, "plan":1}

相关文章

网友评论

      本文标题:【A Tour of Go 练习】:映射

      本文链接:https://www.haomeiwen.com/subject/ppnefhtx.html