-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrndgroups.go
86 lines (68 loc) · 1.42 KB
/
rndgroups.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
package main
import (
"bytes"
"github.com/docopt/docopt-go"
"io/ioutil"
"log"
"math/rand"
"time"
)
func main() {
// Set up docs
usage := `Usage:
rndgroups <filename> <groupsize>
`
arguments, err := docopt.ParseDoc(usage)
if err != nil {
println(err)
}
// Read in file
fileName, _ := arguments.String("<filename>")
stringList := readFile(fileName)
// shuffle
shuffle(stringList, time.Now().UnixNano())
// output in groups
groupSize, _ := arguments.Int("<groupsize>")
printGroups(stringList, groupSize)
}
func shuffle(data []string, seed int64) {
// Get random groups of groupSize from data
rand.Seed(seed)
for i := 0; i < 100000; i++ {
i, j := rand.Intn(len(data)), rand.Intn(len(data))
// swap
temp := data[i]
data[i] = data[j]
data[j] = temp
}
}
func readFile(path string) []string {
content, err := ioutil.ReadFile(path)
if err != nil {
log.Fatal(err)
}
return convertByteSliceToList(content)
}
func convertByteSliceToList(byteSlice []byte) []string {
byteList := bytes.Split(byteSlice, []byte("\n"))
retVal := make([]string, len(byteList))
for i, val := range byteList {
retVal[i] = string(val)
}
return retVal
}
func printGroups(data []string, groupSize int) {
for i := 0; i < len(data); {
for j := 0; j < groupSize; j++ {
if i < len(data) {
print(data[i])
// Omit trailing dash
if j < groupSize-1 {
print(" - ")
}
}
i++
}
print("\n")
}
}