-
Notifications
You must be signed in to change notification settings - Fork 4
/
permutations.go
146 lines (111 loc) · 2.7 KB
/
permutations.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
143
144
145
146
package itertools
//GenPermutations generates, given a number n,
//all the n factorial permutations of the integers
//from 0 to n-1
func GenPermutations(n int) <-chan []int {
if n < 0 {
panic("Invalid argument")
}
ch := make(chan []int)
go func() {
var finished bool
result := make([]int, n)
for i := range result {
result[i] = i
}
temp := make([]int, n)
copy(temp, result) // avoid overwriting of result
ch <- temp
for {
finished = true
for i := n - 1; i > 0; i-- {
if result[i] > result[i-1] {
finished = false
minGreaterIndex := i
for j := i + 1; j < n; j++ {
if result[j] < result[minGreaterIndex] && result[j] > result[i-1] {
minGreaterIndex = j
}
}
result[i-1], result[minGreaterIndex] = result[minGreaterIndex], result[i-1]
//sort from i to n-1
for j := i; j < n; j++ {
for k := j + 1; k < n; k++ {
if result[j] > result[k] {
result[j], result[k] = result[k], result[j]
}
}
}
break
}
}
if finished {
close(ch)
break
}
temp := make([]int, n)
copy(temp, result) // avoid overwriting of result
ch <- temp
}
}()
return ch
}
//PermutationsInt generates all the permutations of r elements
//extracted from an slice of integers
func PermutationsInt(iterable []int, r int) chan []int {
ch := make(chan []int)
go func() {
length := len(iterable)
for comb := range GenCombinations(length, r) {
for perm := range GenPermutations(r) {
result := make([]int, r)
for i := 0; i < r; i++ {
result[i] = iterable[comb[perm[i]]]
}
ch <- result
}
}
close(ch)
}()
return ch
}
//PermutationsStr generates all the permutations of r elements
//extracted from an slice of strings
func PermutationsStr(iterable []string, r int) chan []string {
ch := make(chan []string)
go func() {
length := len(iterable)
for comb := range GenCombinations(length, r) {
for perm := range GenPermutations(r) {
result := make([]string, r)
for i := 0; i < r; i++ {
result[i] = iterable[comb[perm[i]]]
}
ch <- result
}
}
close(ch)
}()
return ch
}
//PermutationsList generates all the permutations of r elements
//extracted from a List (an arbitrary list of elements).
//A List can be created, for instance, as follows:
// myList := List{"a", "b", 13, 3.523}
func PermutationsList(iterable List, r int) chan List {
ch := make(chan List)
go func() {
length := len(iterable)
for comb := range GenCombinations(length, r) {
for perm := range GenPermutations(r) {
result := make(List, r)
for i := 0; i < r; i++ {
result[i] = iterable[comb[perm[i]]]
}
ch <- result
}
}
close(ch)
}()
return ch
}