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 ...
随机推荐
- [Ruby on Rails系列]5、专题:Talk About SaSS
This is my presentation in the Rails lecture of Hosei University. I will introduce SaSS by 5 example ...
- hdu 4557 非诚勿扰
水题…… 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> #include<io ...
- html10个特效(转载)
http://www.html5tricks.com/10-html5-jquery-image-animatin.html 现在网页上的图片已经不再是10几年前那种低像素的静态图片了,有了HTML5 ...
- android webview网页控件
一个WebView的简单例子 .在开发过程中应该注意几点: 1.AndroidManifest.xml中必须使用许可"android.permission.INTERNET",否则 ...
- 一个批量转换jtl文件的shell
最近在项目中遇到了批量转换jmeter测试结果jtl的问题,整了一个脚本,记录如下: #bin/sh filelist=`ls jtl` # 将jtl目录的所有文件列表读取并存入变量 for file ...
- 【零基础学习iOS开发】【02-C语言】11-函数的声明和定义
在上一讲中,简单介绍了函数的定义和使用,只要你想完成一个新功能,首先想到的应该是定义一个新的函数来完成这个功能.这讲继续介绍函数的其他用法和注意事项. 一.函数的声明 1.在C语言中,函数的定义顺序是 ...
- 如何引用传递String Boolean 等,并改变他们的值
如何引用传递String Boolean 等,并改变他们的值 采用list, 在存入位置改变list的值 如 list.add(true); list.remove(0); list.add(fals ...
- 2014年最火的 21个JavaScript 框架
下面,我们将会介绍 2014 年最火的 21 款JavaScript 框架,专为前端开发者准备的哦:)众所周知, JavaScript 框架是 JavaScript编程语言最棒的特性之一. JavaS ...
- Android开发之读写联系人
读写联系人需要用到android的ContentProvider 同时需要读和写联系人的权限 需要使用到联系人数据库中的 * raw_contacts表: * contact_id:联系人id * d ...
- 将非WPF window设为 WPF Window的Owner
如果WPF Content是寄宿在Win32 窗体或Windows Form中,则在WPF模块中可能不会存在WPF Window(WPF模块的根可能是个UserControl).如果在WPF模块中弹出 ...