-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathslow.go
87 lines (71 loc) · 1.75 KB
/
slow.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"bufio"
"encoding/json"
"fmt"
"log"
"math/rand"
"os"
"regexp"
"sort"
"strings"
"time"
)
func main() {
start := time.Now()
input := readStdin()
if input == "" {
fmt.Println("Input needs to be piped via stdin... echo \"blah blah blah\" | go run slow.go")
os.Exit(1)
} else {
process(clean(input))
}
fmt.Printf("Execution took %v seconds\n", time.Since(start).Seconds())
}
func process(words string) {
wordCountSampleMap := make(map[string]float64)
wordCountPopulationEstimateMap := make(map[string]int)
wordArray := strings.Split(words, " ")
iterations := 6000000000
for i := 0; i < iterations; i++ {
index := randRange(0, len(wordArray))
wordCountSampleMap[strings.ToLower(wordArray[index])]++
}
for word, count := range wordCountSampleMap {
wordCountPopulationEstimateMap[word] = int(count/float64(iterations)*float64(len(wordArray)-1)) + 1
}
output(wordCountPopulationEstimateMap)
}
func readStdin() string {
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) == 0 {
var stdin string
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
stdin = scanner.Text()
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
return stdin
}
return ""
}
func output(data map[string]int) {
sortMap(data)
dataAsJson, _ := json.MarshalIndent(data, "", " ")
fmt.Println(string(dataAsJson))
}
func clean(str string) string {
return regexp.MustCompile(`[^a-zA-Z0-9 ]+`).ReplaceAllString(str, "")
}
func randRange(min, max int) int {
return rand.Intn(max-min) + min
}
func sortMap(data map[string]int) {
keys := make([]string, 0, len(data))
for key := range data {
keys = append(keys, key)
}
sort.Slice(keys, func(i, j int) bool { return data[keys[i]] > data[keys[j]] })
}