Array combinations

Hello,

Is there a way to get all combinations of an array of 3 items, whose value is 0 or 1 ?

For instance, for an array of 3 items, it should generate the arrays below :

[ [0,0,0], [1,0,0], [0,1,0], [0,0,1], [1,1,0], [1,0,1], [0,1,1], [1,1,1] ];

Many thanks,
Christophe

allTuples([0,1]!3)

You can also check ‘enum’ from miSCellaneous_lib quark with a lot of enumeration options, above would be

3.enum([0,1])

if all you want is that output, then i’d say that line works quite well. :slight_smile:

it sounds like you are trying to make something more generic, though. do you want all arrays of size n, not just 3? for example.

Yes, exactly, I’m looking for all arrays of size n, I could define as an argument.

Thanks,

Christophe

That’s kinda what functions are for.

So if you have allTuples([0,1]!3) and you want 3 to be parameterized:

f = { |n = 3| allTuples([0, 1] ! n) };

To parameterize the possible values, I’ll leave that “as an exercise for the reader” (as math texts used to say).

hjh