-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate2.go
165 lines (127 loc) · 2.75 KB
/
generate2.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import "fmt"
func combinations(iterable []int, r int) {
pool := iterable
n := len(pool)
if r > n {
return
}
indices := make([]int, r)
for i := range indices {
indices[i] = i
}
result := make([]int, r)
for i, el := range indices {
result[i] = pool[el]
}
fmt.Println(result)
for {
i := r - 1
for ; i >= 0 && indices[i] == i+n-r; i -= 1 {
}
if i < 0 {
return
}
indices[i] += 1
for j := i + 1; j < r; j += 1 {
indices[j] = indices[j-1] + 1
}
for ; i < len(indices); i += 1 {
result[i] = pool[indices[i]]
}
fmt.Println(result)
}
}
func permutations(iterable []int, r int) {
pool := iterable
n := len(pool)
if r > n {
return
}
indices := make([]int, n)
for i := range indices {
indices[i] = i
}
cycles := make([]int, r)
for i := range cycles {
cycles[i] = n - i
}
result := make([]int, r)
for i, el := range indices[:r] {
result[i] = pool[el]
}
fmt.Println(result)
for n > 0 {
i := r - 1
for ; i >= 0; i -= 1 {
cycles[i] -= 1
if cycles[i] == 0 {
index := indices[i]
for j := i; j < n-1; j += 1 {
indices[j] = indices[j+1]
}
indices[n-1] = index
cycles[i] = n - i
} else {
j := cycles[i]
indices[i], indices[n-j] = indices[n-j], indices[i]
for k := i; k < r; k += 1 {
result[k] = pool[indices[k]]
}
fmt.Println(result)
break
}
}
if i < 0 {
return
}
}
}
func product(argsA, argsB []int) {
pools := [][]int{argsA, argsB}
npools := len(pools)
indices := make([]int, npools)
result := make([]int, npools)
for i := range result {
result[i] = pools[i][0]
}
fmt.Println(result)
for {
i := npools - 1
for ; i >= 0; i -= 1 {
pool := pools[i]
indices[i] += 1
if indices[i] == len(pool) {
indices[i] = 0
result[i] = pool[0]
} else {
result[i] = pool[indices[i]]
break
}
}
if i < 0 {
return
}
fmt.Println(result)
}
}
func main() {
fmt.Println("Itertools combinations in Go:")
// combinations('ABCD', 2) --> AB AC AD BC BD CD
// combinations(range(4), 3) --> 012 013 023 123
fmt.Printf("iterable = %s, r = %d", "[]int{1, 2, 3, 4, 5, 6}", 3)
fmt.Println()
combinations([]int{1, 2, 3, 4, 5, 6}, 3)
fmt.Println("Itertools permutations in Go:")
// permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC
// permutations(range(3)) --> 012 021 102 120 201 210
fmt.Printf("iterable = %s, r = %d", "[]int{1, 2, 3, 4}", 3)
fmt.Println()
permutations([]int{1, 2, 3, 4}, 3)
fmt.Println("Itertools product in Go:")
// product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
// product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
fmt.Printf("iterables = %s, %s", "[]int{1, 2, 3}", "[]int{10, 20, 30}")
fmt.Println()
product([]int{1, 2, 3}, []int{10, 20, 30})
}