Last active
August 29, 2015 14:03
-
-
Save gotohr/9be3a4a2c26604a12473 to your computer and use it in GitHub Desktop.
generate all combinations of sets of interface{} sets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
type Set []interface{} | |
func Combinations(s []Set) Set { | |
lens := make([]int, len(s)) | |
length := 1 | |
for i, v := range s { | |
lens[i] = len(v) | |
length = length * lens[i] | |
} | |
pad := 1 | |
res := make([][]int, len(s)) | |
for i := len(s) - 1; i >= 0; i-- { | |
setLength := lens[i] | |
row := make([]int, length) | |
index := 0 | |
for k := 0; k < (length / setLength / pad); k++ { | |
for i := 0; i < setLength; i++ { | |
for p := 0; p < pad; p++ { | |
row[index] = i | |
index++ | |
} | |
} | |
} | |
res[i] = row | |
pad = pad * setLength | |
} | |
r := Set{} | |
for i := 0; i < length; i++ { | |
q := make([]interface{}, len(s)) | |
for j := 0; j < len(s); j++ { | |
q[j] = s[j][res[j][i]] | |
} | |
r = append(r, q) | |
} | |
return r | |
} | |
func main() { | |
s1 := Set{"a", "b", "c"} | |
s2 := Set{1, 2, 3} | |
s3 := Set{"A", "B"} | |
sx := []Set{s1, s2, s3} | |
r := Combinations(sx) | |
fmt.Println(r) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment