I have no idea what this is for but it was here

Signed-off-by: AKP <tom@tdpain.net>
This commit is contained in:
akp 2021-10-19 18:26:50 +01:00
parent da569f4e75
commit fb0872daa6
No known key found for this signature in database
GPG key ID: AA5726202C8879B7
2 changed files with 60 additions and 0 deletions

View 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
View 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)
}
}