Pyth, 23 21 bytes
Ju+G*]GHQYFNJjdmsqdNJ
Input is a list of integers, like [3, 21, 1]1, 2]
. Try it online: Pyth Compiler/Executor
Uses a quite similar idea as randomra's J code. The first part of the code Ju+G*]GHQY
generates n
parts of similar things. For the example input [3, 1, 1, 2]
the result looks like this:
[
[],
[],
[],
[[], [], []],
[[], [], [], [[], [], []]],
[[], [], [], [[], [], []], [[], [], [], [[], [], []]]],
[[], [], [], [[], [], []], [[], [], [], [[], [], []]]]
]
First three identical elements, than one element , then one element again and at the end two identical elements.
Ju+G*]GHQY
u QY reduce the input Q, start with empty list G=[]
for each H in input, replace the value of G by:
+G*]GH G+[G]*H
J store the result in J
The second part of the code is comparing the elements of the Cartesian product and printing it.
FNJjdmsqdNJ
FNJ for N in J:
m J map each element d of J to
qdN the boolean value of d == N
s and convert it to an integer (0 = False, 1 = True)
jd print the resulting list seperated by d (=space)