Permutations

J

Jose

Hi all,

I have a select list that can contain between 1 and N items. I would
like to get all possible permutations of the list, i.e. combinations
that a user might select. So for example if my list has the following
options: Red, Yellow, Blue I would like to get the following list:

Red
Yellow
Blue
Red;Yellow
Red;Blue
Yellow;Blue
Red;Yellow;Blue

I don't want to get any repititions i.e. the permutation "Red;Blue" is
equal to "Blue;Red" (in my eyes!!)

Can anybody help?

Thanks in advance,

Jose
 
L

Larry Lard

Jose said:
Hi all,

I have a select list that can contain between 1 and N items. I would
like to get all possible permutations of the list, i.e. combinations
that a user might select. So for example if my list has the following
options: Red, Yellow, Blue I would like to get the following list:

Red
Yellow
Blue
Red;Yellow
Red;Blue
Yellow;Blue
Red;Yellow;Blue

In the usual terminology, this is neither a list of permutations nor a
list of combinations :) But that doesn't matter now...

A neat way to do this is to loop a number from 1 to 2^(number of things
in the list)-1 (so in this instance, from 1 to 7. Each time round the
loop, express the number in binary:

001
010
011
100
101
110
111

and include in the selection (in this example) 'Red' if the first bit
is set, 'Yellow' if the second bit is set, and 'Blue' if the third bit
is set. If you want to include the case where _no_ items are selected,
just start the loop at 0.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top