A Tour of Go 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.
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的更多相关文章
- A Tour of Go Exercise: Images
Remember the picture generator you wrote earlier? Let's write another one, but this time it will ret ...
- A Tour of Go Exercise: HTTP Handlers
Implement the following types and define ServeHTTP methods on them. Register them to handle specific ...
- A Tour of Go Exercise: Errors
Copy your Sqrt function from the earlier exercises and modify it to return an error value. Sqrt shou ...
- A Tour of Go Exercise: Fibonacci closure
Let's have some fun with functions. Implement a fibonacci function that returns a function (a closur ...
- 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 ...
- 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 ...
- 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 ...
- exercise.tour.go google的go官方教程答案
/* Exercise: Loops and Functions #43 */ package main import ( "fmt" "math" ) fun ...
- Essential controls for web app
AUTO-COMPLETE/AUTO-SUGGEST Auto-complete using Vaadin Offer auto-suggest or auto-complete to help yo ...
随机推荐
- SDUT2191Calendar
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2191 题意:给你两个年月日,让你算出其中经历了 ...
- 如何在DJANGO里获取?带数据的东东,基于CBV
用DEF的,有现成的,而用CLASS的,就要作一下变通. 如下: if self.request.GET: if self.request.GET.get('search_pk'): search_p ...
- ubuntu12 开机自动转到命令行
命令: sudo gedit /etc/default/grub 找到这一行 GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"改成 GRUB_CM ...
- Web Api 入门
http://www.cnblogs.com/developersupport/p/WebAPI-Security.html http://www.cnblogs.com/r01cn/archive/ ...
- Time.deltaTime 含义和应用
第一種:使用Time.deltaTime 一秒內從第1個Frame到最後一個Frame所花的時間,所以不管電腦是一秒跑60格或者一秒30格.24格,值都會趨近於一. 就結果而言,deltaTime是為 ...
- 【转】PostgreSQL IP地址访问配置
原文:http://blog.csdn.net/shuaiwang/article/details/1793294 1.PostgreSQL的安装目录,进入data文件夹,打开postgresql.c ...
- Android的三种网络通信方式
Android平台有三种网络接口可以使用,他们分别是:java.net.*(标准Java接口).Org.apache接口和Android.net.*(Android网络接口).下面分别介绍这些接口的功 ...
- Java面试题-多线程
1. java中有几种方法可以实现一个线程? 多线程有两种实现方法,分别是继承Thread类与实现Runnable接口. 这两种方法的区别是,如果你的类已经继承了其它的类,那么你只能选择实现Runna ...
- poj 1416 Shredding Company( dfs )
我的dfs真的好虚啊……,又是看的别人的博客做的 题目== 题目:http://poj.org/problem?id=1416 题意:给你两个数n,m;n表示最大数,m则是需要切割的数. 切割m,使得 ...
- 解决Eclipse导出javadoc乱码问题
在Eclipse里 export 选 JavaDoc,在向导的最后一页的Extra JavaDoc Options 里填上参数即可 比如项目采用的是UTF-8的编码就填:-encoding UTF-8 ...