combin: Add helpers for dealing with permutations (#1076)

* combin: Add helpers for dealing with permutations
This commit is contained in:
Brendan Tracey
2019-09-11 21:00:27 +01:00
committed by GitHub
parent 1d8f8b2ee4
commit 40d3308efe
3 changed files with 475 additions and 42 deletions

View File

@@ -297,3 +297,45 @@ func TestCartesian(t *testing.T) {
t.Errorf("cartesian data mismatch.\nwant:\n%v\ngot:\n%v", want, got)
}
}
func TestPermutationIndex(t *testing.T) {
for cas, s := range []struct {
n, k int
}{
{6, 3},
{4, 4},
{10, 1},
{8, 2},
} {
n := s.n
k := s.k
perms := make(map[string]struct{})
for i := 0; i < NumPermutations(n, k); i++ {
perm := IndexToPermutation(nil, i, n, k)
idx := PermutationIndex(perm, n, k)
if idx != i {
t.Errorf("Cas %d: permutation mismatch. Want %d, got %d", cas, i, idx)
}
perms[intSliceToKey(perm)] = struct{}{}
}
if len(perms) != NumPermutations(n, k) {
t.Errorf("Case %d: not all generated combinations were unique", cas)
}
}
}
func TestPermutationGenerator(t *testing.T) {
for n := 0; n <= 7; n++ {
for k := 1; k <= n; k++ {
permutations := Permutations(n, k)
pg := NewPermutationGenerator(n, k)
genPerms := make([][]int, 0, len(permutations))
for pg.Next() {
genPerms = append(genPerms, pg.Permutation(nil))
}
if !intSosMatch(permutations, genPerms) {
t.Errorf("Permutations and generated permutations do not match. n = %v, k = %v", n, k)
}
}
}
}