I have no idea what this is for but it was here
Signed-off-by: AKP <tom@tdpain.net>
This commit is contained in:
parent
da569f4e75
commit
fb0872daa6
2 changed files with 60 additions and 0 deletions
34
helpers/cartesianProduct.go
Normal file
34
helpers/cartesianProduct.go
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
package helpers
|
||||||
|
|
||||||
|
// The following two functions were taken and/or modified from https://stackoverflow.com/a/29023392
|
||||||
|
|
||||||
|
func CartesianProduct(a []int, k int) <-chan []int {
|
||||||
|
c := make(chan []int)
|
||||||
|
lens := len(a)
|
||||||
|
r := make([]int, k)
|
||||||
|
|
||||||
|
go func(c chan []int) {
|
||||||
|
defer close(c)
|
||||||
|
for ix := make([]int, k); ix[0] < lens; nextIndex(ix, lens) {
|
||||||
|
for i, j := range ix {
|
||||||
|
r[i] = a[j]
|
||||||
|
}
|
||||||
|
c <- r
|
||||||
|
}
|
||||||
|
}(c)
|
||||||
|
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// nextIndex sets ix to the lexicographically next value,
|
||||||
|
// such that for each i>0, 0 <= ix[i] < lens.
|
||||||
|
func nextIndex(ix []int, lens int) {
|
||||||
|
// https://stackoverflow.com/a/29023392
|
||||||
|
for j := len(ix) - 1; j >= 0; j-- {
|
||||||
|
ix[j]++
|
||||||
|
if j == 0 || ix[j] < lens {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ix[j] = 0
|
||||||
|
}
|
||||||
|
}
|
26
helpers/combinations.go
Normal file
26
helpers/combinations.go
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
package helpers
|
||||||
|
|
||||||
|
|
||||||
|
// The following two functions were taken from https://stackoverflow.com/a/19249957
|
||||||
|
|
||||||
|
func GenerateCombinations(alphabet string, length int) <-chan string {
|
||||||
|
c := make(chan string)
|
||||||
|
go func(c chan string) {
|
||||||
|
defer close(c)
|
||||||
|
addLetter(c, "", alphabet, length)
|
||||||
|
}(c)
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
func addLetter(c chan string, combo string, alphabet string, length int) {
|
||||||
|
if length <= 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var newCombo string
|
||||||
|
for _, ch := range alphabet {
|
||||||
|
newCombo = combo + string(ch)
|
||||||
|
c <- newCombo
|
||||||
|
addLetter(c, newCombo, alphabet, length-1)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue