-
Notifications
You must be signed in to change notification settings - Fork 4
/
wordstat.go
142 lines (124 loc) · 2.47 KB
/
wordstat.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"flag"
"fmt"
"io"
"log"
"os"
"sort"
"strings"
"unicode"
"unicode/utf8"
)
func main() {
log.SetPrefix("wordstat: ")
log.SetFlags(0)
flag.Usage = usage
flag.Parse()
words := flag.Args()
if flag.NArg() < 1 {
buf, err := io.ReadAll(os.Stdin)
if err != nil {
log.Fatal(err)
}
lines := strings.Split(string(buf), "\n")
for _, line := range lines {
spaces := strings.Split(line, " ")
words = append(words, spaces...)
}
}
for _, word := range words {
var ws WS
ws.Analyze(word)
}
}
func usage() {
fmt.Fprintln(os.Stderr, "usage: [options] <word> ...")
flag.PrintDefaults()
os.Exit(2)
}
type WS struct{}
func (ws *WS) Analyze(word string) {
word = strings.TrimSpace(word)
if word == "" {
return
}
fmt.Printf("%q (Length: %d)\n", word, utf8.RuneCountInString(word))
ws.basic(word)
ws.letterdiff(word)
ws.histogram(word)
fmt.Printf("\n")
}
func (ws *WS) histogram(word string) {
m := make(map[rune]int)
for _, r := range word {
m[r]++
}
var p [][2]int64
for k, v := range m {
p = append(p, [2]int64{int64(k), int64(v)})
}
sort.Slice(p, func(i, j int) bool {
if p[i][1] == p[j][1] {
return p[i][0] < p[j][0]
}
return p[i][1] > p[j][1]
})
fmt.Printf("[Histogram]\n")
for _, h := range p {
fmt.Printf("%c: %d\n", h[0], h[1])
}
fmt.Printf("\n")
}
func (ws *WS) basic(word string) {
var sum, mul, xor, and, or, vowel uint
mul = 1
for _, r := range word {
sum += uint(r)
mul *= uint(r)
xor ^= uint(r)
and &= uint(r)
or |= uint(r)
if isvowel(r) {
vowel++
}
}
fmt.Printf("[Basic]\n")
fmt.Printf("Sum: %d %#x\n", sum, sum)
fmt.Printf("Mul: %d %#x\n", mul, mul)
fmt.Printf("XOR: %d %#x\n", xor, xor)
fmt.Printf("AND: %d %#x\n", and, and)
fmt.Printf("OR: %d %#x\n", or, or)
fmt.Printf("Vowels: %d %#x\n", vowel, vowel)
fmt.Printf("Palindrome: %v\n", ispalindrome(word))
}
func (ws *WS) letterdiff(word string) {
fmt.Printf("[Letter Differences, Sum, XOR, AND, OR]\n")
lr := rune(-1)
i := 0
for _, r := range word {
if lr >= 0 {
fmt.Printf("%d: %d %d %d %d %d\n", i, rabs(r-lr), rabs(r+lr), rabs(r^lr), rabs(r&lr), rabs(r|lr))
}
lr = r
i++
}
}
func rabs(x rune) rune {
if x < 0 {
return -x
}
return x
}
func isvowel(r rune) bool {
return strings.IndexRune("aeiou", unicode.ToLower(r)) >= 0
}
func ispalindrome(s string) bool {
r := []rune(s)
for i := 0; i < len(r)/2; i++ {
if r[i] != r[len(r)-i-1] {
return false
}
}
return true
}