Created
April 2, 2012 02:20
-
-
Save tetsuok/2280069 to your computer and use it in GitHub Desktop.
An answer of the exercise: Maps on a tour of Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
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)
}
Que es?
El jue., 13 de mayo de 2021 23:15, Boullon Daniel ***@***.***>
escribió:
… ***@***.**** commented on this gist.
------------------------------
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)
}
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<https://gist.github.com/2280069#gistcomment-3742179>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AST4I6IFZU5UNOUQY4W5MBTTNSBVJANCNFSM4IYSUVXA>
.
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)
}
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)
}
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)
}
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)
}
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)
}
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)
}
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)
}
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
without Split or Fields func
O(n)