Skip to content

Instantly share code, notes, and snippets.

@tetsuok
Created April 2, 2012 02:20
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save tetsuok/2280069 to your computer and use it in GitHub Desktop.
Save tetsuok/2280069 to your computer and use it in GitHub Desktop.
An answer of the exercise: Maps on a tour of Go
package main
import (
"code.google.com/p/go-tour/wc"
"strings"
)
func WordCount(s string) map[string]int {
m := make(map[string]int)
a := strings.Fields(s)
for _, v := range a {
m[v]++
}
return m
}
func main() {
wc.Test(WordCount)
}
@Tusharsb
Copy link

package main

import (
	"golang.org/x/tour/wc"
	"strings"
)

func WordCount(s string) map[string]int {
	//split the sentence into  words
	words := strings.Fields(s)
	
	//instantiate wordcount map
        //where word will be the key, and count will be value
	wordcount := make(map[string]int)

	//loop through each word in sentence..
	for _, element := range words {
		
		// and increment the counter of that word by 1
		wordcount[element] = wordcount[element] + 1
	}

	return wordcount
}

func main() {
	wc.Test(WordCount)
}

@lealLuo
Copy link

lealLuo commented Dec 2, 2018

I think mine is stupid but it works ...

package main

import (
	"golang.org/x/tour/wc"
	"strings"
)

func WordCount(s string) map[string]int {
	fs := strings.Fields(s)
	m := make(map[string]int)
	for _, v := range fs {
            if len(v) == 1 {
                c := strings.Count(s, v + " ")
		m[v] = c
	    } else {
		m[v] = strings.Count(s, v)
	    }	
	}
	return m
}

func main() {
	wc.Test(WordCount)
}

@jhenriquez
Copy link

jhenriquez commented Feb 11, 2019

package main

import (
	"strings"

	"golang.org/x/tour/wc"
)

func WordCount(s string) map[string]int {
	count := make(map[string]int)
	for _, v := range strings.Fields(s) {
		count[v] = (count[v] + 1)
	}
	return count
}

func main() {
	wc.Test(WordCount)
}

@banothurameshnaik
Copy link

package main

import (
	"strings"
	"golang.org/x/tour/wc"
)

func WordCount(s string) map[string]int {
	m := make(map[string]int)
	wordsList := strings.Fields(s)
	for _, word := range wordsList {
		m[word]++
	}
	return m
}

func main() {
	wc.Test(WordCount)
}

@everbslab
Copy link

package main

import (
	"golang.org/x/tour/wc"
	"strings"
)

func WordCount(s string) map[string]int {
	m := make(map[string]int)
	fields := strings.Fields(s)
	
	for _, v := range fields {
		m[v]++
	}
	
	return m
}

func main() {
	wc.Test(WordCount)
}

@olehcambel
Copy link

package main

import (
	"strings"

	"golang.org/x/tour/wc"
)

// WordCount comment
func WordCount(s string) map[string]int {
	fields := strings.Fields(s)
	result := map[string]int{}

	for _, v := range fields {
		if _, ok := result[v]; ok {
			result[v]++
		} else {
			result[v] = 1
		}
	}

	return result
}

func main() {
	wc.Test(WordCount)
}

@kaen98
Copy link

kaen98 commented Apr 20, 2020


package main

import (
	"strings"
	"golang.org/x/tour/wc"
)

func WordCount(s string) map[string]int {
	var sList = strings.Fields(s)
	var wordCount = make(map[string]int)
	for _, v := range sList {
		wordCount[v] = wordCount[v] + 1
	}
	return wordCount
}

func main() {
	wc.Test(WordCount)
}

@Andrew-He
Copy link

package main

import (
	"golang.org/x/tour/wc";
	"strings";
)

func WordCount(s string) map[string]int {
	m := make(map[string]int)
	a := strings.Fields(s)
	for _, v := range(a) {
		m[v]++
	}
	return m
}

func main() {
	wc.Test(WordCount)
}

@denddyprod
Copy link

denddyprod commented May 17, 2020

I had some problems with testing function wc.test so I tested it manually

package main

import "strings"
import "fmt"

func WordCount(s string) map[string]int {
	m := make(map[string]int)

	out := strings.Split(s, " ")

	for _, val := range out {
		m[val]++
	}


	return m
}

func main() {
	m := make(map[string]int)
	m = WordCount("This is how I count a lot of words and I test function of counting words")
	fmt.Println(m)
}

@atyrian
Copy link

atyrian commented Jun 14, 2020

package main

import (
"golang.org/x/tour/wc"
"strings"
)

func WordCount(s string) map[string]int {
words := strings.Fields(s)
m := make(map[string]int, len(words))
for _,v := range words{
m[v] += 1
}
return m
}

func main() {
wc.Test(WordCount)
}

@geekyarthurs
Copy link

package main

import (
	"strings"

	"golang.org/x/tour/wc"
)

func WordCount(s string) map[string]int {

	counts := make(map[string]int)
	words := strings.Fields(s)
	
	for i := range words{
	
			counts[words[i]]++
	
	}
	return counts
}

func main() {
	wc.Test(WordCount)
}

@coderhh
Copy link

coderhh commented Jun 25, 2020

Here is my answer:

package main

import (
	"golang.org/x/tour/wc"
	"strings"
)

func WordCount(s string) map[string]int {
	m := make(map[string]int)
	r := strings.Fields(s)
	for _, v := range r {
		_,ok:= m[v]
		if ok {
		   m[v] = m[v] +1
		}else{
		   m[v] = 1
		}
	}
	return m
}

func main() {
	wc.Test(WordCount)
}

@Raghav2211
Copy link

Raghav2211 commented Sep 7, 2020

package main

import (
	"golang.org/x/tour/wc"
	"strings"
)

func WordCount(s string) map[string]int {
	splitInput := strings.Split(s," ")
	wordCount := make(map[string]int)
	for _ , r := range splitInput {
		wordCount[r] = wordCount[r]+1
	}

	return wordCount
}

func main() {
	wc.Test(WordCount)
}

@albertleng
Copy link

package main

import (
	"golang.org/x/tour/wc"
	"strings"
)

func WordCount(s string) map[string]int {
    words := strings.Fields(s)
	wordCount := make(map[string]int)
	
	for _, word := range words {
	    if _, exist := wordCount[word]; exist == true {
		    wordCount[word] += 1
		} else {
		    wordCount[word] = 1
		}
	}
	return wordCount
}

func main() {
	wc.Test(WordCount)
}

@hefedev
Copy link

hefedev commented Mar 5, 2021

package main

import (
	"strings"
	"golang.org/x/tour/wc"
)

func WordCount(s string) map[string]int {
	f := strings.Fields(s)
	wc := make(map[string]int)
	
	for _, v := range f {
		wc[v]++
	}
	
	return wc
}

func main() {
	wc.Test(WordCount)
}

@ichiro-ss
Copy link

without "strings"

I'm a beginner so please give me some advice

package main

import (
	"golang.org/x/tour/wc"
)

func WordCount(s string) map[string]int {
	var word string
	ans := make(map[string]int)
	for i, w := range s {
		if string(w) == " "{
			ans[word]++
			word = ""
		}else if i == len(s)-1 {
			word += string(w)
			ans[word]++
			word = ""
		}else{
			word += string(w)
		}
	}
	return ans
}

func main() {
	wc.Test(WordCount)
}

@ramesh-km
Copy link

package main

import (
	"strings"

	"golang.org/x/tour/wc"
)

func WordCount(s string) map[string]int {
	words := strings.Fields(s)
	wordCounts := make(map[string]int)
	for _, v := range words {
		wordCounts[v]++
	}
	return wordCounts
}

func main() {
	wc.Test(WordCount)
}

@xfelmutx
Copy link

xfelmutx commented Apr 2, 2021

package main

import (
	"strings"
	"golang.org/x/tour/wc"
)

func WordCount(s string) map[string]int {
	var sentence []string = strings.Fields(s)
	var m = make(map[string]int)
	var edited bool = false

	for _, v := range sentence {
		for key, i := range m {
			if key == "" {
				edited = false
			} else if v == key && key != "" {
				i++
				m[v] = i
				edited = true
			}
		}
		if !edited {
			m[v] = 1
		} else {
			edited = false
		}
	}
	return m
}

func main() {
	wc.Test(WordCount)
}

@coderinblack08
Copy link

package main

import (
	"golang.org/x/tour/wc"
	"strings"
)

func WordCount(s string) map[string]int {
	m := make(map[string]int)
	for _, a := range strings.Split(s, " ") {
		v, ok := m[a]
		if ok {
			m[a] = v + 1
		} else {
			m[a] = 1
		}
	}
	return m
}

func main() {
	wc.Test(WordCount)
}

@DrCoffee84
Copy link

without Split or Fields func

O(n)

package main

import (
	"golang.org/x/tour/wc"
)

// Return a map of the counts of each “word” in the string s.
func WordCount(s string) (wordsMap map[string]int) {

	wordsMap = make(map[string]int)
	sizeS := len(s)
	j := 0
	for i := 0; i < sizeS; i++ {
		// you can replace this condition with regex or multiple condition with differents caracters, but the test only consider the spaces.
		if s[i] == ' ' {
			wordsMap[s[:i]]++
			s = s[(i + 1):]
			sizeS -= j + 1
			i -= j
			j = 0
		}
		j++
	}
	wordsMap[s]++
	return
}

// The wc.Test function runs a test suite against the provided function and prints success or failure.
func main() {
	wc.Test(WordCount)
}

@DonkovtsevArthur
Copy link

DonkovtsevArthur commented Nov 19, 2021

package main

import (
	"golang.org/x/tour/wc"
	"strings"
)

func WordCount(s string) map[string]int {
	m:= map[string]int{}

	for _,v:= range strings.Fields(s) {
		m[v]++
	}
	
	return m
}

func main() {
	wc.Test(WordCount)
}

@laura-brizuela
Copy link

laura-brizuela commented Nov 19, 2021 via email

@Devdha
Copy link

Devdha commented Dec 14, 2021

package main

import (
	"strings"
	"golang.org/x/tour/wc"
)

func WordCount(s string) map[string]int {
	var m = map[string]int {}
	for _, w:= range strings.Fields(s) {
		_, ok := m[w]
		if (ok) {
			m[w] += 1
		} else {
			m[w] = 1
		}
	}
	return m
}

func main() {
	wc.Test(WordCount)
}

@Rexoen
Copy link

Rexoen commented Aug 7, 2022

package main

import (
	"strings"
	"golang.org/x/tour/wc"
)

func WordCount(s string) map[string]int {
	m := make(map[string]int)
	for _,w:= range strings.Split(s," "){
		_,ok := m[w]
		if ! ok {
			m[w] = 1
		} else {
			m[w] += 1
		}
	}
	return m
}

func main() {
	wc.Test(WordCount)
}

@krapie
Copy link

krapie commented Dec 7, 2022

package main

import (
	"golang.org/x/tour/wc"
	"strings"
)

func WordCount(s string) map[string]int {
	m := make(map[string]int)
	
	ss := strings.Fields(s)
	for _, word := range ss {
		_, exists := m[word]
		
		if exists {
			m[word]++
		} else {
			m[word] = 1
		}
	}
	
	return m
}

func main() {
	wc.Test(WordCount)
}

@alkstsgv
Copy link

package main

import (
	"strings"
	"golang.org/x/tour/wc"
)

func WordCount(s string) map[string]int {
	a := []string{s}
	var b map[string]int
	b = make(map[string]int)
	for _, el := range a {
		a1 := strings.Fields(el)
		for e1, e2 := range a1 {
			e1 = b[e2]
			if e1 == e1 {
				b[e2] = e1 + 1
			}
		}
	}
	return b
}

func main() {
	wc.Test(WordCount)
}

@0riginaln0
Copy link

package main

import (
	"strings"
	"golang.org/x/tour/wc"
)

func WordCount(s string) map[string]int {
	words := strings.Fields(s)
	words_map := make(map[string]int)

	for _, word := range words {
		if count, is_present := words_map[word]; is_present {
			words_map[word] = count + 1
		} else {
			words_map[word] = 1
		}
	}
	return words_map
}

func main() {
	wc.Test(WordCount)
}

@sno-windy
Copy link

Here is my struggling answer without strings.Fields.

package main

import "golang.org/x/tour/wc"

func WordCount(s string) map[string]int {
	result := make(map[string]int)
	words := make([]string, 0)
	var start, end, wordLen int
	for i, rune := range s {
		wordLen += 1
		if string(rune) != " " {
			continue
		}
		end = i
		words = append(words, s[start:end])
		start = i+1
		wordLen = 0
	}
	if wordLen > 0 {
		words = append(words, s[start:len(s)])
	}	
	for _, v := range words {
		_, ok := result[v]
		if ok {
			result[v] += 1
		} else {
			result[v] = 1
		}
	}
	return result
}

func main() {
	wc.Test(WordCount)
}

@poirierunited
Copy link

package main

import (
"golang.org/x/tour/wc"
"strings"
)

func WordCount(s string) map[string]int {
counterMap := make(map[string]int)

for _, word := range strings.Fields(s) {
	value, isPresent := counterMap[word]
	
	if isPresent {
		counterMap[word] = value + 1
	
	} else {
		counterMap[word] = 1
	}
}

return counterMap

}

func main() {
wc.Test(WordCount)
}

@j4breu
Copy link

j4breu commented Mar 13, 2024

package main

import (
	"golang.org/x/tour/wc"
	"strings"
)

func WordCount(s string) map[string]int {
	count := make(map[string]int)
	
	for _, word := range strings.Fields(s) {
		count[word]++
	}
	
	return count
}

func main() {
	wc.Test(WordCount)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment