Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Permuting using any number of given chars (http://www.velocityreviews.com/forums/t892409-permuting-using-any-number-of-given-chars.html)

 Brian Wakem 05-17-2005 08:51 PM

Permuting using any number of given chars

I can use Algorithm::Permute to output all permutations of 1 2 3

123
132
312
213
231
321

But what I'm looking for is all possible permutations using any number of
the input chars.

Like this -

1
2
3
12
13
21
23
31
32
123
132
312
213
231
321

I can't think of a way right now and I can't find a module that does it.
Any ideas?

--
Brian Wakem

 Anno Siegel 05-17-2005 10:21 PM

Re: Permuting using any number of given chars

Brian Wakem <no@email.com> wrote in comp.lang.perl.misc:
> I can use Algorithm::Permute to output all permutations of 1 2 3
>
> 123
> 132
> 312
> 213
> 231
> 321
>
>
> But what I'm looking for is all possible permutations using any number of
> the input chars.
>
> Like this -
>
> 1
> 2
> 3
> 12
> 13
> 21
> 23
> 31
> 32
> 123
> 132
> 312
> 213
> 231
> 321
>
>
> I can't think of a way right now and I can't find a module that does it.

Split the problem. You know how to get all permutations for a given
set. So find all subsets of the input characters ('', '3', '2', '23', '1',
'13', '12', '123') and apply your solution to each.

for ( subsets( '123') ) {
print_permutations( \$_);
}

The definition of subsets() is left as an exercise in recursion.

Anno

 All times are GMT. The time now is 08:58 PM.