package main
import (
"fmt"
)
func main() {
a := []int{1,2}
b := []int{3,4}
c := []int{5,6}
d := []int{7,8,9}
//e := []int{1,2}
seq := [][]int{a, b, c, d} // slice to combine with comb [[b1 b2] [c1 c2 c3]]
fmt.Println("final comb ", combIntSlices(seq) )
}
// function to build the combinations
func combIntSlices(seq [][]int) (out [][]int) {
// fill combSeq with the first slice of seq
var combSeq [][]int // combSeq is the initial [][]int, for example [[1] [2] [3]]
for _, i := range seq[0] {
combSeq = append(combSeq, []int{i})
}
seq = seq[1:] // seq is the [][]slice to combine with combSeq [[4 5] [6 7 8]]
fmt.Println(seq, len(seq))
// rec recursive funtion
var rec func(int, [][]int, [][]int)
rec = func(i int, seq, combSeq [][]int) {
var temp [][]int // temporary slice to append combinations
last := len(seq) - 1
fmt.Println(combSeq, "-", seq)
for j, c := range combSeq { // for each slice in combSeq slice
for k, s := range seq[i] { // append each element of the slice i in seq
c1 := append(c, s)
temp = append(temp, c1)
fmt.Println(i,j,k, "- temp =", temp, " c=", c)
}
combSeq = temp // at this step temp has recorded one round of combination
}
if i == last { // if the length of seq is reached, the solution is returned
out = combSeq
return
} else {
rec(i+1, seq, combSeq) // if the length of seq is not reached, rec is called to perform another step of combinations
}
}
rec(0, seq, combSeq) // start the first step of combinations
return out
}