当前位置:网站首页>学习 Go 语言 0x05:《Go 语言之旅》中映射(map)的练习题代码
学习 Go 语言 0x05:《Go 语言之旅》中映射(map)的练习题代码
2022-04-23 11:02:00 【爱博客大伯】
* 题目:使用映射(map)实现WordCount
https://tour.go-zh.org/moretypes/23
练习:映射
实现 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)
m := make(map[string]int)
for _, word := range words {
n, ok := m[word]
if ok == false {
m[word] = 1
} else {
m[word] = n + 1
}
}
return m
}
func main() {
wc.Test(WordCount)
}
运行结果:
$ ./word_count.exe
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}
对代码做小的改动,运行结果跟上面一样,如下:
package main
import (
"golang.org/x/tour/wc"
"strings"
)
func WordCount(s string) map[string]int {
words := strings.Fields(s)
m := make(map[string]int)
for _, word := range words {
_, ok := m[word] // 更改了这里,不再赋给临时变量
if ok == false {
m[word] = 1
} else {
m[word]++ // 更改了这里,直接对m[word]的值加1
}
}
return m
}
func main() {
wc.Test(WordCount)
}
版权声明
本文为[爱博客大伯]所创,转载请带上原文链接,感谢
https://blog.csdn.net/u013553529/article/details/88903864
边栏推荐
- More reliable model art than deep learning
- 语雀文档编辑器将开源:始于但不止于Markdown
- Gets the current time in character format
- 解决方案架构师的小锦囊 - 架构图的 5 种类型
- Solutions to common problems in visualization (IX) background color
- Cve-2019-0708 vulnerability exploitation of secondary vocational network security 2022 national competition
- Mysql8.0安装指南
- Introduction to neo4j authoritative guide, recommended by Qiu Bojun, Zhou Hongxiang, Hu Xiaofeng, Zhou Tao and other celebrities
- MBA-day5数学-应用题-工程问题
- ID number verification system based on visual structure - Raspberry implementation
猜你喜欢
VIM + ctags + cscope development environment construction guide
CUMCM 2021-B:乙醇偶合制備C4烯烴(2)
Cumcm 2021 - B: préparation d'oléfines C4 par couplage éthanol (2)
Chapter 120 SQL function round
MySQL how to merge the same data in the same table
After the MySQL router is reinstalled, it reconnects to the cluster for boot - a problem that has been configured in this host before
Mysql8.0安装指南
Deploy jar package
Diary of dishes | Blue Bridge Cup - hexadecimal to octal (hand torn version) with hexadecimal conversion notes
Intuitive understanding entropy
随机推荐
The songbird document editor will be open source: starting with but not limited to markdown
VIM usage
Alarm scene recognition
Embedded related surface (I)
Visualized common drawing (II) line chart
VIM + ctags + cscope development environment construction guide
Learning website materials
Xdotool key Wizard
Source insight 4.0 FAQs
图像处理——噪声小记
MBA-day6 逻辑学-假言推理练习题
【leetcode】107.二叉树的层序遍历II
面向全球市场,PlatoFarm今日登录HUOBI等全球四大平台
Solution architect's small bag - 5 types of architecture diagrams
Excel·VBA自定义函数获取单元格多数值
web三大组件(Servlet,Filter,Listener)
主流手机分辨率与尺寸
软件测试人员,如何优秀的提Bug?
Leetcode22: bracket generation
Introduction to neo4j authoritative guide, recommended by Qiu Bojun, Zhou Hongxiang, Hu Xiaofeng, Zhou Tao and other celebrities