-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
47 lines (42 loc) · 1015 Bytes
/
main.go
File metadata and controls
47 lines (42 loc) · 1015 Bytes
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
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 a slice of slice of string to hold slices of words
buckets := make([][]string, 12)
// loop over the words
for scanner.Scan() {
word := scanner.Text()
n := hashBucket(word, 12)
buckets[n] = append(buckets[n], word)
}
// print len of each bucket
for i := 0; i < 12; i++ {
fmt.Println(i, "-", len(buckets[i]))
}
// print the words in one of the buckets
// fmt.Println(buckets[6])
fmt.Println("len -", len(buckets))
fmt.Println("cap -", cap(buckets))
}
func hashBucket(word string, buckets int) int {
var sum int
for _, v := range word {
sum += int(v)
}
return sum % buckets
}