Re: Permutations
- From: "Larry Lard" <larrylard@xxxxxxxxxxx>
- Date: 23 Mar 2006 06:34:00 -0800
Jose wrote:
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.
--
Larry Lard
Replies to group please
.
- References:
- Permutations
- From: Jose
- Permutations
- Prev by Date: Re: change object reference
- Next by Date: Re: Accessing Arraylists
- Previous by thread: Permutations
- Next by thread: change object reference
- Index(es):
Relevant Pages
|