How to generate row-wise permutations of a 2d Array?
Solution 1:
You're really just permuting the row order, and then flattening the matrix for each permutation. Python example:
from itertools import chain, permutations
def flatten(matrix):
return list(chain(*matrix))
def permute(matrix):
return [flatten(perm) for perm in permutations(matrix)]
With your example:
>>> M = [[1], [5, 2], [6]]
>>> permute(M)
[[1, 5, 2, 6], [1, 6, 5, 2], [5, 2, 1, 6], [5, 2, 6, 1], [6, 1, 5, 2], [6, 5, 2, 1]]
In another language, you may have to implement your own helper functions to iterate through permutations (possibly using recursion), and list flattening.