码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • Go语言学习笔记-A Tour of Go 练习笔记-Maps


    Exercise: Maps

    题目:

    Implement WordCount. It should return a map of the counts of each “word” in the string s. The wc.Test function runs a test suite against the provided function and prints success or failure.

    You might find strings.Fields helpful.

    练习程序:

    1. package main
    2. import (
    3. "golang.org/x/tour/wc"
    4. "strings"
    5. )
    6. var string_count_map map[string]int
    7. func WordCount(s string) map[string]int {
    8. string_count_map = make(map[string]int)
    9. string_count := strings.Fields(s)
    10. for _, j := range string_count{
    11. ok := false
    12. _, ok = string_count_map[j]
    13. if ok{
    14. string_count_map[j] += 1
    15. }else{
    16. string_count_map[j] = 1
    17. }
    18. }
    19. return string_count_map
    20. }
    21. func main() {
    22. wc.Test(WordCount)
    23. }

    运行结果:

    1. PASS
    2. f("I am learning Go!") =
    3. map[string]int{"Go!":1, "I":1, "am":1, "learning":1}
    4. PASS
    5. f("The quick brown fox jumped over the lazy dog.") =
    6. map[string]int{"The":1, "brown":1, "dog.":1, "fox":1, "jumped":1, "lazy":1, "over":1, "quick":1, "the":1}
    7. PASS
    8. f("I ate a donut. Then I ate another donut.") =
    9. map[string]int{"I":2, "Then":1, "a":1, "another":1, "ate":2, "donut.":2}
    10. PASS
    11. f("A man a plan a canal panama.") =
    12. map[string]int{"A":1, "a":2, "canal":1, "man":1, "panama.":1, "plan":1}

    笔记:该题目旨在通过Maps的创建和赋值,从而实现对String中单词的计数功能,通过利用string.Fields将一个字符串进行切分,得到单词slice, 讲该slice中的每个单词作为map的key值进行赋值,而map的value则根据单词出现的次数进行自增+1计数。

  • 相关阅读:
    阅读openfoam框图
    一文了解JVM(中)
    对Transformer中的Attention(注意力机制)的一点点探索
    python知识点总结(1)
    视频集中存储/云存储/磁盘阵列EasyCVR平台分组批量绑定/取消设备功能详解
    碳中和大潮惊涛拍岸,科技企业如何迈入这条大江大河?
    【C++初阶】三、类和对象(中)
    如何按照一定的需求进行开发ALV报表
    40+ Node.js 常见面试问题 [2024]
    Pytest使用fixture实现token共享
  • 原文地址:https://blog.csdn.net/sxmatch/article/details/127807844
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号