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. SQLite入门与分析(二)---设计与概念(续)

    SQLite入门与分析(二)---设计与概念(续)   写在前面:本节讨论事务,事务是DBMS最核心的技术之一.在计算机科学史上,有三位科学家因在数据库领域的成就而获ACM图灵奖,而其中之一Jim G ...

  2. ANDROID_MARS学习笔记_S01_012_RatingBar

    1.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns: ...

  3. Android开发经验记录

    一.    代码规范 定一个规范的主要目的,是为了让不同的开发人员写的代码能保持一致性,方便别人看自己的代码.另外,对个人来说,也能起到让自己看着舒服的作用. 1.      基本 * 使用UTF-8 ...

  4. 去掉php框架CI默认url中的index.php

    CI默认的rewrite url中是类似这样的 例如你的CI根目录是在/CodeIgniter/下,你的下面的二级url就类似这样 http://localhost/CodeIgniter/index ...

  5. 25.allegro中模块复用[原创]

    一,Module reuse 1,打开原理图 ------------------- --------------------- ctrl+i过滤器 直选part ctrl+e 查看属性 查看: 是否 ...

  6. 摄像头(4)用Camera和SurfaceView自定义拍照程序

    定制拍照程序的基本步骤 1,打开照相机:Camera.open 这是独占方式打开的 2,创建SurfaceView对象 多缓冲,多线程view 3,添加回调事件监听器(SurfaceHolder.ad ...

  7. html5自带表单验证-美化改造

    神奇的代码 暂且叫做html5.css /* === HTML5 validation styles === */ .myform select:required, .myform input:req ...

  8. LBS云端数据删除和上传

    这里采用C#模拟表单提交,实现LBS云端删除和csv格式文件的上传. 删除: /// <summary> /// 从LBS云端删除数据 /// </summary> /// & ...

  9. POJ 2318 (叉积) TOYS

    题意: 有一个长方形,里面从左到右有n条线段,将矩形分成n+1个格子,编号从左到右为0~n. 端点分别在矩形的上下两条边上,这n条线段互不相交. 现在已知m个点,统计每个格子中点的个数. 分析: 用叉 ...

  10. PopupWindow-弹窗的界面

      1 效果图 2 知识点 PopupWindow(View contentView, int width, int height) //创建一个没有获取焦点.长为width.宽为height,内容为 ...