当前位置:网站首页>Learn go language 0x05: the exercise code of map in go language journey
Learn go language 0x05: the exercise code of map in go language journey
2022-04-23 11:08:00 【Love blog uncle】
* subject : Use mapping (map) Realization WordCount
https://tour.go-zh.org/moretypes/23
practice : mapping
Realization WordCount. It should return a map , It contains the string s Each of them “ word ” The number of . function wc.Test A series of test cases will be executed for this function , And output success or failure .
You'll find that strings.Fields Very helpful .
* Code
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)
}
Running results :
$ ./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}
Make small changes to the code , The running result is the same as above , as follows :
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] // Changed here , No longer assigned to temporary variables
if ok == false {
m[word] = 1
} else {
m[word]++ // Changed here , Direct pair m[word] The value of the add 1
}
}
return m
}
func main() {
wc.Test(WordCount)
}
版权声明
本文为[Love blog uncle]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231102105955.html
边栏推荐
- 闹钟场景识别
- Structure of C language (Advanced)
- Mba-day5 Mathematics - application problems - engineering problems
- Mba-day6 logic - hypothetical reasoning exercises
- Use of SVN:
- Detailed explanation of MySQL creation stored procedure and function
- Solutions to common problems in visualization (VIII) solutions to problems in shared drawing area
- Solution architect's small bag - 5 types of architecture diagrams
- Full stack cross compilation x86 completion process experience sharing
- 学习 Go 语言 0x08:《Go 语言之旅》中 练习使用 error
猜你喜欢
Mysql8. 0 installation guide
Excel·VBA数组冒泡排序函数
《Neo4j权威指南》简介,求伯君、周鸿袆、胡晓峰、周涛等大咖隆重推荐
Solutions to common problems in visualization (VIII) solutions to problems in shared drawing area
Mysql8.0安装指南
Google Earth Engine(GEE)——将原始影像进行升尺度计算(以海南市为例)
Typora operation skill description (I) md
Cygwin 中的 rename 用法
More reliable model art than deep learning
GO接口使用
随机推荐
Differences among restful, soap, RPC, SOA and microservices
Microsoft Access database using PHP PDO ODBC sample
Mysql8.0安装指南
Excel·VBA自定义函数获取单元格多数值
How to use JDBC callablestatement The wasnull () method is called to check whether the value of the last out parameter is SQL null
软件测试人员,如何优秀的提Bug?
数据库管理软件SQLPro for SQLite for Mac 2022.30
Analysis on the characteristics of the official game economic model launched by platoffarm
使用 PHP PDO ODBC 示例的 Microsoft Access 数据库
Pycharm
CUMCM 2021-b: preparation of C4 olefins by ethanol coupling (2)
FileProvider 路径配置策略的理解
Visualization Road (11) detailed explanation of Matplotlib color
Typora operation skill description (I) md
vm设置静态虚拟机
Use of SVN:
MySQL索引优化之分页探索详细介绍
MySQL8. 0 upgraded stepping on the pit Adventure
《Neo4j权威指南》简介,求伯君、周鸿袆、胡晓峰、周涛等大咖隆重推荐
Mysql中有关Datetime和Timestamp的使用总结