当前位置:网站首页>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
边栏推荐
- Strongest date regular expression
- How to bind a process to a specified CPU
- VScode
- Mba-day5 Mathematics - application problems - engineering problems
- CUMCM 2021-b: preparation of C4 olefins by ethanol coupling (2)
- VIM usage
- Visualization Road (11) detailed explanation of Matplotlib color
- 解决 『SunCertPathBuilderException:unable to find valid certification path to requested target』 问题
- Mysql中一千万条数据怎么快速查询
- STM32接电机驱动,杜邦线供电,然后反烧问题
猜你喜欢
随机推荐
Resolution and size of mainstream mobile phones
Latex usage
期货开户哪个公司好?安全靠谱的期货公司谁能推荐几家?
CUMCM 2021-B:乙醇偶合制備C4烯烴(2)
Detailed explanation of typora Grammar (I)
Prevent SQL injection in web projects
Xdotool key Wizard
进程间通信 -- 消息队列
Introduction to neo4j authoritative guide, recommended by Qiu Bojun, Zhou Hongxiang, Hu Xiaofeng, Zhou Tao and other celebrities
Oracle连通性测试小工具
About the three commonly used auxiliary classes of JUC
FileProvider 路径配置策略的理解
详解MySQL中timestamp和datetime时区问题导致做DTS遇到的坑
More reliable model art than deep learning
Visual Road (XII) detailed explanation of collection class
@valid,@Validated 的学习笔记
Visualization Road (11) detailed explanation of Matplotlib color
An interesting interview question
MySQL interview questions explain how to set hash index
STM32接电机驱动,杜邦线供电,然后反烧问题








