Re: Permutations

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




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

.



Relevant Pages

  • Re: how to use perms when my n is greater than 10?
    ... I had a similar problem and I thought I use a reverse process to ... generate the permutations one at a time and use it in a loop from 1 ... permutations by imagining a loop generating the permutations. ... since n nested loops would generate the combination. ...
    (comp.soft-sys.matlab)
  • Re: balanced REDUCE: a challenge for the brave
    ... Composition of permutations can be used. ... loop -rot 2drop; ... swap over swap hshift swap r> hshift +; ...
    (comp.lang.forth)
  • Re: Combinations and permutations
    ... taken r at a time is any selection of r of the elements, ... So therefore, combinations are for unordered sets, and permutations ... "ordered sets" are a bit of a misnomer, ...
    (sci.math)
  • Re: Combinations and permutations
    ... taken r at a time is any selection of r of the elements, ... So therefore, combinations are for unordered sets, and permutations ... this straight in my head first) ... # permutations without replacement ...
    (sci.math)
  • Re: is this a good way to find anagrams?
    ... > letters in your given word. ... Then loop through the word file once. ... Even faster - sort the list of permutations, ... list, then repeat, setting the lower bound of your search to the ...
    (comp.lang.ruby)