-
Notifications
You must be signed in to change notification settings - Fork 23
/
main.go
66 lines (61 loc) · 1.83 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/******************************************************************************
* Execution: go run cmd/frequency-counter/main.go L < input.txt
* Data files: https://algs4.cs.princeton.edu/31elementary/tinyTale.txt
* https://algs4.cs.princeton.edu/31elementary/tale.txt
* https://algs4.cs.princeton.edu/31elementary/leipzig100K.txt
* https://algs4.cs.princeton.edu/31elementary/leipzig300K.txt
* https://algs4.cs.princeton.edu/31elementary/leipzig1M.txt
*
* Read in a list of words from standard input and print out
* the most frequently occurring word that has length greater than
* a given threshold.
*
* % go run cmd/frequency-counter/main.go 1 < tinyTale.txt
* it 10
*
* % go run cmd/frequency-counter/main.go 8 < tale.txt
* business 122
*
* % go run cmd/frequency-counter/main.go 10 < leipzig1M.txt
* government 24763
*
*
******************************************************************************/
package main
import (
"fmt"
"os"
"strconv"
"github.com/shellfly/algo/algs4"
"github.com/shellfly/algo/stdin"
)
func main() {
minLen, _ := strconv.Atoi(os.Args[1])
//st := algs4.NewSequentialSearchST()
//st := algs4.NewBinarySearchST()
//st := algs4.NewBST()
// st := algs4.NewRedBlackBST()
//st := algs4.NewSeparateChainHashST(0)
st := algs4.NewLinearProbingHashST(0)
stdin := stdin.NewStdIn()
for !stdin.IsEmpty() {
word := stdin.ReadString()
if len(word) < minLen {
continue
}
wordKey := algs4.StringHashKey(word)
if !st.Contains(wordKey) {
st.Put(wordKey, 1)
} else {
st.Put(wordKey, st.GetInt(wordKey)+1)
}
}
max := algs4.StringHashKey("")
st.Put(max, 0)
for _, word := range st.Keys() {
if st.GetInt(word.(algs4.StringHashKey)) > st.GetInt(max) {
max = word.(algs4.StringHashKey)
}
}
fmt.Printf("%s %d", max, st.GetInt(max))
}