-
Notifications
You must be signed in to change notification settings - Fork 1
/
match_words.go
60 lines (55 loc) · 1.29 KB
/
match_words.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
// -----------------------------------------------------------------------------
// CMDX Utilities Suite cmdx/[match_words.go]
// (c) [email protected] License: GPLv3
// -----------------------------------------------------------------------------
package main
import (
"strings"
"github.com/balacode/zr"
)
// matchWords _ _
func matchWords(cmd Command, args []string) {
if len(args) != 2 {
env.Println("requires <word-length> and <letter-set> parameters")
return
}
length := zr.Int(args[0])
letterSet := args[1]
// load word list
var words []string
{
data, done := env.ReadFile(WordListFile)
if !done {
return
}
s := string(data)
s = strings.ReplaceAll(s, "\r\n", "\n")
for strings.Contains(s, "\n\n") {
s = strings.ReplaceAll(s, "\n\n", "\n")
}
words = strings.Split(s, "\n")
}
for _, word := range words {
if len(word) != length {
continue
}
word = strings.ToLower(word)
var (
used = 0
letters = strings.Split(letterSet, "")
)
for _, wordLetter := range strings.Split(word, "") {
for i, letter := range letters {
if wordLetter == letter {
used++
letters[i] = "0"
break
}
}
if used == length {
env.Printf(" %s ", word)
}
}
}
}
// end