forked from GoesToEleven/GolangTraining
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
49 lines (45 loc) · 1.06 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main
import (
"bufio"
"fmt"
"log"
"net/http"
)
func main() {
// get the book adventures of sherlock holmes
res, err := http.Get("http://www.gutenberg.org/cache/epub/1661/pg1661.txt")
if err != nil {
log.Fatal(err)
}
// scan the page
scanner := bufio.NewScanner(res.Body)
defer res.Body.Close()
// Set the split function for the scanning operation.
scanner.Split(bufio.ScanWords)
// Create map with a key of int
// and a value of another map
// with a key of string, which will be the word
// and a value of int, which will be the number of times the word occurs
buckets := make(map[int]map[string]int)
// Create slices to hold words words
for i := 0; i < 12; i++ {
buckets[i] = make(map[string]int)
}
// Loop over the words
for scanner.Scan() {
word := scanner.Text()
n := hashBucket(word, 12)
buckets[n][word]++
}
// Print words in a bucket
for k, v := range buckets[6] {
fmt.Println(v, " \t- ", k)
}
}
func hashBucket(word string, buckets int) int {
var sum int
for _, v := range word {
sum += int(v)
}
return sum % buckets
}