Re: how to print all the combinations ??




"Rob Thorpe" <robert.thorpe@xxxxxxxxxxxx> wrote in message
news:1143535101.568487.38860@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Mark P wrote:
Maciej Pilichowski wrote:
On 27 Mar 2006 22:29:20 -0800, "sudharsan" <suderson.tk@xxxxxxxxx>
wrote:

for a given set of numbers how to print all sort of combinations
for example if a[]={1,2,3};
need to print :- 123,132,213,231,312,321

So you need to print all the _permutations_. Now, when you know what
you are looking for use google.


Some languages can do this very easily, for example, C++ has the
standard library function next_permutation. Otherwise you'll need to
develop an equivalent function on your own.

It isn't that hard. You just need to run 3 loops, each checking that
the value used is a perm.

Ie. generate
111
112
113
121
122
123
131
132

and pick 123, 132 etc by preventing each nested loop from using digits
already used by those it is enclosed by.

next_permutation is of-course useful though.


Yes if you limit the number to 3, but I don't think that was the OPs intent.
Adding another loop for each number isn't reasonable even for fairly small
numbers. Plus, although the example should the set being consecutive
integers, I would not assume this was a requirement.


.



Relevant Pages

  • Re: Cases where Forth seems a little clunky
    ... complexity of stack shuffling by the complexity of exotic operators. ... with Forth derived languages under RPN. ... that one (I'm lying a bit, I double checked the loop more thoroughly a ...
    (comp.lang.forth)
  • Re: Very poor Lisp performance
    ... Observe LOOP, ... than many Lisp users, as LOOP is offensive to a percentage of Lisp ... >>> natural and programming languages have complicated grammars precisely ... do you agree that languages are evolving to be more concise? ...
    (comp.lang.lisp)
  • Re: Cases where Forth seems a little clunky
    ... complexity of stack shuffling by the complexity of exotic operators. ... with Forth derived languages under RPN. ... that one (I'm lying a bit, I double checked the loop more thoroughly a ...
    (comp.lang.forth)
  • Re: Question about loop
    ... The basic question is can loop be used to write ... Is Common Lisp one of those languages that pretends DELETE is a high ... There has been at least one conforming implementation of Common Lisp ... Sequence may be destroyed and used to construct the result; ...
    (comp.lang.lisp)
  • Re: Looking for a way to convert dec to bin
    ... It's set in stone now for vb but yes, many languages won't work. ... designed languages the loop var is out of scope anyway. ... This loop will probably never exit because b is always 255 or less. ... Generally if I need the index then I won't use foreach. ...
    (microsoft.public.vb.general.discussion)