当前位置:网站首页>学习 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
边栏推荐
- Download and installation steps of xshell + xftp
- 面向全球市场,PlatoFarm今日登录HUOBI等全球四大平台
- Alarm scene recognition
- ffmpeg命令行常用参数
- Using El popconfirm and El backtop does not take effect
- ID number verification system based on visual structure - Raspberry implementation
- CUMCM 2021-B:乙醇偶合制备C4烯烃(2)
- Go interface usage
- Visualized common drawing (II) line chart
- Ueditor -- limitation of 4m size of image upload component
猜你喜欢
Google Earth engine (GEE) - scale up the original image (taking Hainan as an example)
Cumcm 2021 - B: préparation d'oléfines C4 par couplage éthanol (2)
Manjaro installation and configuration (vscode, wechat, beautification, input method)
【leetcode】107. Sequence traversal of binary tree II
Visual common drawing (IV) histogram
More reliable model art than deep learning
Diary of dishes | Blue Bridge Cup - hexadecimal to octal (hand torn version) with hexadecimal conversion notes
Reading integrity monitoring techniques for vision navigation systems - 5 Results
Solutions to common problems in visualization (VIII) solutions to problems in shared drawing area
Learning note 5 - gradient explosion and gradient disappearance (k-fold cross verification)
随机推荐
Notes on concurrent programming of vegetables (IX) asynchronous IO to realize concurrent crawler acceleration
Learning Notes 6 - Summary of several deep learning convolutional neural networks
How to quickly download vscode
解决方案架构师的小锦囊 - 架构图的 5 种类型
The difference between restful and soap
Analysis on the characteristics of the official game economic model launched by platoffarm
MBA-day5数学-应用题-工程问题
Special members and magic methods
Ueditor -- limitation of 4m size of image upload component
A diary of dishes | 238 Product of arrays other than itself
Read integrity monitoring techniques for vision navigation systems
web三大组件(Servlet,Filter,Listener)
Mba-day5 Mathematics - application problems - engineering problems
colab
Understand the key points of complement
App. In wechat applet JS files, components, APIs
Dirichlet prefix sum (number theory optimization formula sub complexity weapon)
最强日期正则表达式
SQL Server cursor circular table data
Installing MySQL with CentOS / Linux