当前位置:网站首页>学习 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. 删除有序数组中的重复项
- Pycharm
- Introduction to neo4j authoritative guide, recommended by Qiu Bojun, Zhou Hongxiang, Hu Xiaofeng, Zhou Tao and other celebrities
- A diary of dishes | 238 Product of arrays other than itself
- Simple thoughts on the design of a microblog database
- Promise详解
- Using El popconfirm and El backtop does not take effect
- Installing MySQL with CentOS / Linux
- Difference between pregnancy box and delivery box
- Solutions to common problems in visualization (VIII) solutions to problems in shared drawing area
猜你喜欢

Excel·VBA自定义函数获取单元格多数值

ID number verification system based on visual structure - Raspberry implementation

使用 PHP PDO ODBC 示例的 Microsoft Access 数据库

UEditor之——图片上传组件大小4M的限制

精彩回顾|「源」来如此 第六期 - 开源经济与产业投资

How can swagger2 custom parameter annotations not be displayed

Solutions to common problems in visualization (IX) background color

Visualization Road (10) detailed explanation of segmentation canvas function

Diary of dishes | Blue Bridge Cup - hexadecimal to octal (hand torn version) with hexadecimal conversion notes

解决方案架构师的小锦囊 - 架构图的 5 种类型
随机推荐
使用zerotier让异地设备组局域网
Visual Road (XII) detailed explanation of collection class
ConstraintLayout布局
Charles function introduction and use tutorial
数据库管理软件SQLPro for SQLite for Mac 2022.30
Restful、SOAP、RPC、SOA、微服务之间的区别
Software testers, how to mention bugs?
Ueditor -- limitation of 4m size of image upload component
Google Earth engine (GEE) - scale up the original image (taking Hainan as an example)
Mba-day5 Mathematics - application problems - engineering problems
妊娠箱和分娩箱的区别
Visual common drawing (V) scatter diagram
Esp32 learning - add folder to project
Facing the global market, platefarm today logs in to four major global platforms such as Huobi
Chapter 1 of technical Xiaobai (express yourself)
使用 PHP PDO ODBC 示例的 Microsoft Access 数据库
SWAT - Introduction to Samba web management tool
MBA-day5數學-應用題-工程問題
Excel·VBA数组冒泡排序函数
vm设置静态虚拟机