-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
61 lines (47 loc) · 1.16 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
package main
import (
"flag"
"log"
"os"
"github.com/joshuacrew/anagram-solver/formatter"
"github.com/joshuacrew/anagram-solver/sectionscanner"
"github.com/joshuacrew/anagram-solver/anagrams"
)
func main() {
var filePath string
flag.StringVar(&filePath, "file_path", "", "file path of words to find anagrams for")
flag.Parse()
file := openFile(filePath)
defer file.Close()
scanner := sectionscanner.New(file)
formatter := formatter.New(os.Stdout)
for {
wordsOfSameLength, err := scanner.Scan()
if err != nil {
log.Fatalf("failed to read file %s: %v", filePath, err)
}
if isEndOfFile(wordsOfSameLength) {
break
}
anagramSet := anagrams.Find(wordsOfSameLength)
for _, words := range anagramSet {
err := formatter.Print(words)
if err != nil {
log.Fatalf("failed to write words to output %s: %v", words, err)
}
}
}
}
func openFile(filePath string) *os.File {
if filePath == "" {
log.Fatal("file path is empty")
}
file, err := os.Open(filePath)
if err != nil {
log.Fatalf("failed to open file %s: %v", filePath, err)
}
return file
}
func isEndOfFile(wordsOfSameLength []string) bool {
return len(wordsOfSameLength) == 0
}