当前位置:网站首页>学习 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
边栏推荐
- 26. 删除有序数组中的重复项
- Mysql8.0安装指南
- STM32接电机驱动,杜邦线供电,然后反烧问题
- Google Earth Engine(GEE)——将原始影像进行升尺度计算(以海南市为例)
- Introduction to neo4j authoritative guide, recommended by Qiu Bojun, Zhou Hongxiang, Hu Xiaofeng, Zhou Tao and other celebrities
- Ueditor -- limitation of 4m size of image upload component
- ConstraintLayout布局
- Intuitive understanding entropy
- Special members and magic methods
- A diary of dishes | 238 Product of arrays other than itself
猜你喜欢
Excel·VBA自定义函数获取单元格多数值
Excel·VBA数组冒泡排序函数
Visual solutions to common problems (VIII) mathematical formulas
Chapter 120 SQL function round
Wonderful review | deepnova x iceberg meetup online "building a real-time data Lake based on iceberg"
The courses bought at a high price are open! PHPer data sharing
Visual common drawing (III) area map
Solutions to common problems in visualization (VIII) solutions to problems in shared drawing area
Learning Notes 6 - Summary of several deep learning convolutional neural networks
Comparison and practice of prototype design of knowledge service app
随机推荐
Excel · VBA array bubble sorting function
Gets the current time in character format
Visual Road (XII) detailed explanation of collection class
Qinglong panel pull library command update [April 20, 2022] collection is not lost
【leetcode】107. Sequence traversal of binary tree II
图像处理——噪声小记
How to use JDBC callablestatement The wasnull () method is called to check whether the value of the last out parameter is SQL null
Introduction to data analysis 𞓜 kaggle Titanic mission (IV) - > data cleaning and feature processing
vm设置静态虚拟机
Notes on concurrent programming of vegetables (IX) asynchronous IO to realize concurrent crawler acceleration
Visual common drawing (V) scatter diagram
MySQL how to merge the same data in the same table
Excel·VBA自定义函数获取单元格多数值
Visual common drawing (I) stacking diagram
Learning notes 7-depth neural network optimization
Typora operation skill description (I) md
Embedded related surface (I)
MySQL Router重装后重新连接集群进行引导出现的——此主机中之前已配置过的问题
Introduction to data analysis 𞓜 kaggle Titanic mission (III) - > explore data analysis
How to quickly download vscode