all combinations

all combinations

am 02.10.2007 01:49:36 von Petr Vileta

Maybe this can be a little off topic, so I'm sorry ;-)

I have an array of strings, say
@array = ['a','b','c'];
and I need to call subroutine or use some module to get array of arrays with
all possible combinations. Good message is that all strings in @array must
be used everytime. This reduce number of combinations.
So the result should be
@result = [
['a','b','c'],
['a','c','b'],
['b','a','c'],
['b','c','a'],
['c','a','b'],
['c','b','a']
];

Please can anybody help me to resolve this problem? I'm bad in mathematics
and I don't know how to ask Google. In other word I'm too stupid to resolve
this ;-)
--

Petr Vileta, Czech republic
(My server rejects all messages from Yahoo and Hotmail. Send me your mail
from another non-spammer site please.)

Re: all combinations

am 02.10.2007 02:28:01 von Bob Walton

Petr Vileta wrote:
....
> I have an array of strings, say
> @array = ['a','b','c'];
> and I need to call subroutine or use some module to get array of arrays
> with all possible combinations. Good message is that all strings in
> @array must be used everytime. This reduce number of combinations.
> So the result should be
> @result = [
> ['a','b','c'],
> ['a','c','b'],
> ['b','a','c'],
> ['b','c','a'],
> ['c','a','b'],
> ['c','b','a']
> ];
>
> Please can anybody help me to resolve this problem? I'm bad in
> mathematics and I don't know how to ask Google. In other word I'm too
> stupid to resolve this ;-)

Well, I think what you want is permutations, not combinations, per your
example. And that's a FAQ:

perldoc -q permute

HTH.
--
Bob Walton
Email: http://bwalton.com/cgi-bin/emailbob.pl

Re: all combinations

am 02.10.2007 05:41:10 von Mintcake

On Oct 2, 6:49 am, "Petr Vileta" wrote:
> Maybe this can be a little off topic, so I'm sorry ;-)
>
> I have an array of strings, say
> @array = ['a','b','c'];
> and I need to call subroutine or use some module to get array of arrays with
> all possible combinations. Good message is that all strings in @array must
> be used everytime. This reduce number of combinations.
> So the result should be
> @result = [
> ['a','b','c'],
> ['a','c','b'],
> ['b','a','c'],
> ['b','c','a'],
> ['c','a','b'],
> ['c','b','a']
> ];
>
> Please can anybody help me to resolve this problem? I'm bad in mathematics
> and I don't know how to ask Google. In other word I'm too stupid to resolve
> this ;-)
> --
>
> Petr Vileta, Czech republic
> (My server rejects all messages from Yahoo and Hotmail. Send me your mail
> from another non-spammer site please.)

If you don't want to use the CPAN module and you're not too worried
about performance the following little program will do what you want.

#!/usr/local/bin/perl

use strict;
use warnings;

my @array = ('a','b','c');

my @result = permute(@array);

print '[' . join(',', map "'$_'", @$_) . "]\n" foreach @result;

sub permute {
return \@_ if @_ == 1;
my @result;
print "@_\n";
for (my $i = 0; $i < @_; $i++) {
my @array = @_;
my $c = splice @array, $i, 1;
push @result, map [$c, @$_], permute(@array);
}
return @result;
}

Re: all combinations

am 02.10.2007 05:54:11 von Mintcake

On Oct 2, 10:41 am, Mintcake wrote:
> On Oct 2, 6:49 am, "Petr Vileta" wrote:
>
>
>
>
>
> > Maybe this can be a little off topic, so I'm sorry ;-)
>
> > I have an array of strings, say
> > @array = ['a','b','c'];
> > and I need to call subroutine or use some module to get array of arrays with
> > all possible combinations. Good message is that all strings in @array must
> > be used everytime. This reduce number of combinations.
> > So the result should be
> > @result = [
> > ['a','b','c'],
> > ['a','c','b'],
> > ['b','a','c'],
> > ['b','c','a'],
> > ['c','a','b'],
> > ['c','b','a']
> > ];
>
> > Please can anybody help me to resolve this problem? I'm bad in mathematics
> > and I don't know how to ask Google. In other word I'm too stupid to resolve
> > this ;-)
> > --
>
> > Petr Vileta, Czech republic
> > (My server rejects all messages from Yahoo and Hotmail. Send me your mail
> > from another non-spammer site please.)
>
> If you don't want to use the CPAN module and you're not too worried
> about performance the following little program will do what you want.
>
> #!/usr/local/bin/perl
>
> use strict;
> use warnings;
>
> my @array = ('a','b','c');
>
> my @result = permute(@array);
>
> print '[' . join(',', map "'$_'", @$_) . "]\n" foreach @result;
>
> sub permute {
> return \@_ if @_ == 1;
> my @result;
> print "@_\n";
> for (my $i = 0; $i < @_; $i++) {
> my @array = @_;
> my $c = splice @array, $i, 1;
> push @result, map [$c, @$_], permute(@array);
> }
> return @result;
>
>
>
> }- Hide quoted text -
>
> - Show quoted text -- Hide quoted text -
>
> - Show quoted text -

Sorry I left a spurious print statement in the sub. Also note that
the original initialisation of @array should be ('a','b','c') not
['a','b','c'].

Re: all combinations

am 02.10.2007 05:55:23 von Peter Makholm

"Petr Vileta" writes:

> I have an array of strings, say
> @array = ['a','b','c'];
> and I need to call subroutine or use some module to get array of
> arrays with all possible combinations.

You might want to look at Algorithm::Permute

//Makholm

Re: all combinations

am 02.10.2007 06:06:44 von Petr Vileta

Mintcake wrote:
> On Oct 2, 6:49 am, "Petr Vileta" wrote:
>> I have an array of strings, say
>> @array = ['a','b','c'];
[...]
>> So the result should be
>> @result = [
>> ['a','b','c'],
>> ['a','c','b'],
>> ['b','a','c'],
>> ['b','c','a'],
>> ['c','a','b'],
>> ['c','b','a']
>> ];
>>
> #!/usr/local/bin/perl
>
> use strict;
> use warnings;
>
> my @array = ('a','b','c');
>
> my @result = permute(@array);
>
> print '[' . join(',', map "'$_'", @$_) . "]\n" foreach @result;
>
> sub permute {
> return \@_ if @_ == 1;
> my @result;
> print "@_\n";
> for (my $i = 0; $i < @_; $i++) {
> my @array = @_;
> my $c = splice @array, $i, 1;
> push @result, map [$c, @$_], permute(@array);
> }
> return @result;
> }

--
Thanks a lot for your example. This is exactly what I need.
--

Petr Vileta, Czech republic
(My server rejects all messages from Yahoo and Hotmail. Send me your mail
from another non-spammer site please.)

Re: all combinations

am 02.10.2007 11:33:46 von Michele Dondi

On Tue, 2 Oct 2007 01:49:36 +0200, "Petr Vileta"
wrote:

>I have an array of strings, say
>@array = ['a','b','c'];
>and I need to call subroutine or use some module to get array of arrays with
>all possible combinations. Good message is that all strings in @array must
>be used everytime. This reduce number of combinations.
>So the result should be
>@result = [
> ['a','b','c'],
> ['a','c','b'],
> ['b','a','c'],
> ['b','c','a'],
> ['c','a','b'],
> ['c','b','a']
>];
>
>Please can anybody help me to resolve this problem? I'm bad in mathematics
>and I don't know how to ask Google. In other word I'm too stupid to resolve
>this ;-)

These are called *permutations*.

perldoc -q permute


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^ ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

Re: all combinations

am 02.10.2007 15:28:18 von Petr Vileta

Mintcake wrote:
> On Oct 2, 10:41 am, Mintcake wrote:
>> On Oct 2, 6:49 am, "Petr Vileta" wrote:
>>> @result = [
>>> ['a','b','c'],
>>> ['a','c','b'],
>>> ['b','a','c'],
>>> ['b','c','a'],
>>> ['c','a','b'],
>>> ['c','b','a']
>>> ];
>>
>
> Sorry I left a spurious print statement in the sub. Also note that
> the original initialisation of @array should be ('a','b','c') not
> ['a','b','c'].
Yes I know. I wrote output example as my debuger show it. I use Komodo.
--

Petr Vileta, Czech republic
(My server rejects all messages from Yahoo and Hotmail. Send me your mail
from another non-spammer site please.)

Re: all combinations

am 02.10.2007 15:36:05 von Petr Vileta

Michele Dondi wrote:
> On Tue, 2 Oct 2007 01:49:36 +0200, "Petr Vileta"
> wrote:
>
>> I have an array of strings, say
>> @array = ['a','b','c'];
>> and I need to call subroutine or use some module to get array of
>> arrays with all possible combinations. Good message is that all
>> strings in @array must be used everytime. This reduce number of
>> combinations.
>> So the result should be
>> @result = [
>> ['a','b','c'],
>> ['a','c','b'],
>> ['b','a','c'],
>> ['b','c','a'],
>> ['c','a','b'],
>> ['c','b','a']
>> ];
>>
>> Please can anybody help me to resolve this problem? I'm bad in
>> mathematics and I don't know how to ask Google. In other word I'm
>> too stupid to resolve this ;-)
>
> These are called *permutations*.
>
As I said, I'm bad in mathematics. I learned mathematics first 3 years only
(from 4 years of education) on technical college, 35 years ago :-)
--

Petr Vileta, Czech republic
(My server rejects all messages from Yahoo and Hotmail. Send me your mail
from another non-spammer site please.)

Re: all combinations

am 02.10.2007 19:58:38 von Michele Dondi

On Tue, 2 Oct 2007 15:36:05 +0200, "Petr Vileta"
wrote:

>>> Please can anybody help me to resolve this problem? I'm bad in
>>> mathematics and I don't know how to ask Google. In other word I'm
>>> too stupid to resolve this ;-)
>>
>> These are called *permutations*.
>>
>As I said, I'm bad in mathematics. I learned mathematics first 3 years only
>(from 4 years of education) on technical college, 35 years ago :-)

I was not accusing you and you don't need to apologize. I'm just
pointing out the correct mathematical term.


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^ ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,