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.

package main

import (
"code.google.com/p/go-tour/wc"
"strings"
) func WordCount(s string) map[string]int {
m := make(map[string]int)
strs := strings.Fields(s)
for _,value := range strs {
v, ok := m[value]
if !ok {
m[value] =
}else{
m[value] = v +
}
}
return m
} func main() {
wc.Test(WordCount)
}
package main

import (
"code.google.com/p/go-tour/wc"
"strings"
) func WordCount(s string) map[string]int {
m := make(map[string]int)
strs := strings.Fields(s)
for i := ; i < len(strs); i++ {
v, ok := m[strs[i]]
if !ok {
m[strs[i]] =
}else{
m[strs[i]] = v +
}
}
return m
} func main() {
wc.Test(WordCount)
}

A Tour of Go Exercise: Maps的更多相关文章

  1. A Tour of Go Exercise: Images

    Remember the picture generator you wrote earlier? Let's write another one, but this time it will ret ...

  2. A Tour of Go Exercise: HTTP Handlers

    Implement the following types and define ServeHTTP methods on them. Register them to handle specific ...

  3. A Tour of Go Exercise: Errors

    Copy your Sqrt function from the earlier exercises and modify it to return an error value. Sqrt shou ...

  4. A Tour of Go Exercise: Fibonacci closure

    Let's have some fun with functions. Implement a fibonacci function that returns a function (a closur ...

  5. A Tour of Go Mutating Maps

    Insert or update an element in map m: m[key] = elem Retrieve an element: elem = m[key] Delete an ele ...

  6. A Tour of Go Exercise: Slices

    Implement Pic. It should return a slice of length dy, each element of which is a slice of dx 8-bit u ...

  7. A Tour of Go Exercise: Loops and Functions

    As a simple way to play with functions and loops, implement the square root function using Newton's ...

  8. exercise.tour.go google的go官方教程答案

    /* Exercise: Loops and Functions #43 */ package main import ( "fmt" "math" ) fun ...

  9. Essential controls for web app

    AUTO-COMPLETE/AUTO-SUGGEST Auto-complete using Vaadin Offer auto-suggest or auto-complete to help yo ...

随机推荐

  1. 91. Decode Ways

    题目: A message containing letters from A-Z is being encoded to numbers using the following mapping: ' ...

  2. python学习链接

    http://www.cnblogs.com/dkblog/archive/2011/06/24/2089026.html 异常处理 http://xiagu1.iteye.com/blog/6195 ...

  3. POJ3608(旋转卡壳--求两凸包的最近点对距离)

    题目:Bridge Across Islands 分析:以下内容来自:http://blog.csdn.net/acmaker/article/details/3178696 考虑如下的算法, 算法的 ...

  4. win设置壁纸

    默认壁纸图片位置: C:\Windows\Web\Wallpaper\Scenes 你可以自己建文件夹,放自己喜欢的桌面壁纸. 设置壁纸: 桌面右键  ->  个性化 然后点击 “桌面背景” - ...

  5. Hibernate映射之实体映射<转载>

    实体类与数据库之间存在某种映射关系,Hibernate依据这种映射关系完成数据的存取,因此映射关系的配置在Hibernate中是最关键的.Hibernate支持xml配置文件与@注解配置两种方式.xm ...

  6. 函数buf_LRU_get_free_only

    /******************************************************************//** Returns a free block from th ...

  7. poj2406 周期

    脑残wa了一次 var s:ansistring; ans,i,k,m:longint; pre:..] of longint; function max(x,y:longint):longint; ...

  8. jquery自动焦点图

    效果预览:http://runjs.cn/detail/vydinibc  带左右箭头的自动焦点图:http://runjs.cn/detail/wr1d1keh html: <div clas ...

  9. [JS前端开发] js/jquery控制页面动态加载数据 滑动滚动条自动加载事件

    页面滚动动态加载数据,页面下拉自动加载内容 相信很多人都见过瀑布流图片布局,那些图片是动态加载出来的,效果很好,对服务器的压力相对来说也小了很多 有手机的相信都见过这样的效果:进入qq空间,向下拉动空 ...

  10. 内存映射 madvise mmap

    http://linux.die.net/man/2/madvise mmap && madvise的配合使用 mmap和madvise一起使用例子 mmap的作用是将硬盘文件的内容映 ...