I have a matrix A
:
[ [0, 1, 0]
[6, 0, 20]
[0, 0, 0]
[1, 11, 0] ]
And a list B
:
[12, 34, 25, 9]
I want to iterate over all possible permutations that assign elements in B
to the positions in A
with value 0
.
All nonzero elements in A
must remain in their initial positions.
The number of zero-valued cells in A
will never be less than the size of B
. B
contains only nonzero elements.
Treat each element in B
as unique, but not necessarily distinct. So if two elements in B
are equal, still treat the two as separate elements that must appear in A
It does not matter if an element k
in B
already appears in A
; each permutation must place k
in some zero-valued position within A
.
So I'd like to iterate over these 2D arrays:
[ [12, 1, 34]
[6, 25, 20]
[9, 0, 0]
[1, 11, 0] ],
[ [12, 1, 34]
[6, 25, 20]
[0, 9, 0]
[1, 11, 0] ],
.
.
(many permutations later)
.
.
[ [0, 1, 0]
[6, 0, 20]
[12, 34, 25]
[1, 11, 9] ]
The generator must definitely include all 2D arrays that DO NOT maintain the original order of list B
row-major wise.