Skip to content

Commit 57ab9b3

Browse files
committed
Create pass_test.go
1 parent 825dc56 commit 57ab9b3

File tree

1 file changed

+243
-0
lines changed

1 file changed

+243
-0
lines changed

test/pass_test.go

+243
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
package test
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
"testing"
7+
)
8+
9+
const (
10+
strNumber = "0123456789"
11+
strAz = "abcdefghijklmnopqrstuvwxyz"
12+
strAZ = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
13+
)
14+
15+
func TestPass(t *testing.T) {
16+
17+
results := PermutationsStr(strings.Split(strNumber, ""), 4)
18+
19+
for s := range results {
20+
fmt.Printf("%#v\n", strings.Join(s, ""))
21+
}
22+
}
23+
24+
func gen(words []string, minLen, maxLen int, check func(pwd string) bool) {
25+
//sourceLen := len(words)
26+
//for passLen := minLen; passLen <= maxLen; minLen++ {
27+
// go gen2(words, passLen, check)
28+
//}
29+
}
30+
31+
func gen2(words []string, passLen int, check func(pwd string) bool) {
32+
//sourceLen := len(words)
33+
//for sI := 0; sI < sourceLen; sI++ {
34+
// source := sourceList[0]
35+
//
36+
//}
37+
}
38+
39+
func gen3(word string, passLen int, check func(pwd string) bool) {
40+
//wordLen := len(word)
41+
//for i := 0; i < wordLen; i++ {
42+
// char := word[i]
43+
//}
44+
}
45+
46+
func gen4(word string, passLen int, check func(pwd string) bool) {
47+
//for {
48+
//
49+
//}
50+
//sourceLen := len(words)
51+
//for i := 0; i < passLen; i++ {
52+
// source := sourceList[0]
53+
//
54+
//}
55+
}
56+
57+
// GenCombinations generates, from two natural numbers n > r,
58+
// all the possible combinations of r indexes taken from 0 to n-1.
59+
// For example if n=3 and r=2, the result will be:
60+
// [0,1], [0,2] and [1,2]
61+
func GenCombinations(n, r int) <-chan []int {
62+
63+
if r > n {
64+
panic("Invalid arguments")
65+
}
66+
67+
ch := make(chan []int)
68+
69+
go func() {
70+
result := make([]int, r)
71+
for i := range result {
72+
result[i] = i
73+
}
74+
75+
temp := make([]int, r)
76+
copy(temp, result) // avoid overwriting of result
77+
ch <- temp
78+
79+
for {
80+
for i := r - 1; i >= 0; i-- {
81+
if result[i] < i+n-r {
82+
result[i]++
83+
for j := 1; j < r-i; j++ {
84+
result[i+j] = result[i] + j
85+
}
86+
temp := make([]int, r)
87+
copy(temp, result) // avoid overwriting of result
88+
ch <- temp
89+
break
90+
}
91+
}
92+
if result[0] >= n-r {
93+
break
94+
}
95+
}
96+
close(ch)
97+
98+
}()
99+
return ch
100+
}
101+
102+
// CombinationsStr generates all the combinations of r elements
103+
// extracted from an slice of strings
104+
func CombinationsStr(iterable []string, r int) chan []string {
105+
106+
ch := make(chan []string)
107+
108+
go func() {
109+
110+
length := len(iterable)
111+
112+
for comb := range GenCombinations(length, r) {
113+
result := make([]string, r)
114+
for i, val := range comb {
115+
result[i] = iterable[val]
116+
}
117+
ch <- result
118+
}
119+
120+
close(ch)
121+
}()
122+
return ch
123+
}
124+
125+
// GenPermutations generates, given a number n,
126+
// all the n factorial permutations of the integers
127+
// from 0 to n-1
128+
func GenPermutations(n int) <-chan []int {
129+
if n < 0 {
130+
panic("Invalid argument")
131+
}
132+
133+
ch := make(chan []int)
134+
135+
go func() {
136+
var finished bool
137+
138+
result := make([]int, n)
139+
140+
for i := range result {
141+
result[i] = i
142+
}
143+
144+
temp := make([]int, n)
145+
copy(temp, result) // avoid overwriting of result
146+
ch <- temp
147+
148+
for {
149+
finished = true
150+
151+
for i := n - 1; i > 0; i-- {
152+
153+
if result[i] > result[i-1] {
154+
finished = false
155+
156+
minGreaterIndex := i
157+
for j := i + 1; j < n; j++ {
158+
if result[j] < result[minGreaterIndex] && result[j] > result[i-1] {
159+
minGreaterIndex = j
160+
}
161+
162+
}
163+
164+
result[i-1], result[minGreaterIndex] = result[minGreaterIndex], result[i-1]
165+
166+
//sort from i to n-1
167+
for j := i; j < n; j++ {
168+
for k := j + 1; k < n; k++ {
169+
if result[j] > result[k] {
170+
result[j], result[k] = result[k], result[j]
171+
}
172+
173+
}
174+
}
175+
break
176+
}
177+
}
178+
179+
if finished {
180+
close(ch)
181+
break
182+
}
183+
temp := make([]int, n)
184+
copy(temp, result) // avoid overwriting of result
185+
ch <- temp
186+
187+
}
188+
189+
}()
190+
return ch
191+
}
192+
193+
// PermutationsStr generates all the permutations of r elements
194+
// extracted from an slice of strings
195+
func PermutationsStr(iterable []string, r int) chan []string {
196+
197+
ch := make(chan []string)
198+
199+
go func() {
200+
201+
length := len(iterable)
202+
203+
for comb := range GenCombinations(length, r) {
204+
for perm := range GenPermutations(r) {
205+
result := make([]string, r)
206+
for i := 0; i < r; i++ {
207+
result[i] = iterable[comb[perm[i]]]
208+
}
209+
ch <- result
210+
}
211+
}
212+
213+
close(ch)
214+
}()
215+
return ch
216+
}
217+
218+
// PermutationsList generates all the permutations of r elements
219+
// extracted from a List (an arbitrary list of elements).
220+
// A List can be created, for instance, as follows:
221+
// myList := List{"a", "b", 13, 3.523}
222+
func PermutationsList(iterable []any, r int) chan []any {
223+
224+
ch := make(chan []any)
225+
226+
go func() {
227+
228+
length := len(iterable)
229+
230+
for comb := range GenCombinations(length, r) {
231+
for perm := range GenPermutations(r) {
232+
result := make([]any, r)
233+
for i := 0; i < r; i++ {
234+
result[i] = iterable[comb[perm[i]]]
235+
}
236+
ch <- result
237+
}
238+
}
239+
240+
close(ch)
241+
}()
242+
return ch
243+
}

0 commit comments

Comments
 (0)