Permutation with a twist ??

M

marora

Hey,

I am looking for an algorithm to do as follows:

my @array = qw (A B C); # This array may have several parameters, for
ex. A B C D

I will like to generate following (not sure if this will be called
permutation):

<NULL>
C
B
A
B C
A C
A B
A B C

Available permutation algorithms generate a different output, for
example:
b c a
c b a
c a b
a c b
b a c
a b c

Any suggestions!

Thanks in advance.

Manish
 
J

John Bokma

Hey,

I am looking for an algorithm to do as follows:

my @array = qw (A B C); # This array may have several parameters, for
ex. A B C D

I will like to generate following (not sure if this will be called
permutation):

<NULL>
C
B
A
B C
A C
A B
A B C

isn't that all k-combinations of @array for k 1 .. length array?
 
B

Brian McCauley

Hey,

I am looking for an algorithm to do as follows:

my @array = qw (A B C); # This array may have several parameters, for
ex. A B C D

I will like to generate following (not sure if this will be called
permutation):

<NULL>
C
B
A
B C
A C
A B
A B C

Available permutation algorithms generate a different output, for
example:
b c a
c b a
c a b
a c b
b a c
a b c

Any suggestions!

Are you worried about speed? If not you can just combine off the shelf
algorithms

use Math::Combinatorics;
my @comb = map { permute @$_ } map { combine ($_, @array) } 1 ..
@array;
 
B

Brian McCauley

Are you worried about speed? If not you can just combine off the shelf
algorithms

use Math::Combinatorics;
my @comb = map { permute @$_ } map { combine ($_, @array) } 1 ..
@array;

Opps, the OP just wanted:

my @comb = map { combine ($_, @array) } 1 .. @array;
 
M

marora

Opps, the OP just wanted:

my @comb = map { combine ($_, @array) } 1 .. @array;- Hide quoted text -

- Show quoted text -

Since I don't have "Math::Combinatorics" installed (nor do I have
permission to install it), I am wondering if there is an alternative.
 
M

Mirco Wahab

Since I don't have "Math::Combinatorics" installed (nor do I have
permission to install it), I am wondering if there is an alternative.

In this case, you could use
a simple ugly hack like this

...

sub bts {
(my $g = unpack 'b*', pack 'j', shift) =~ s/0+$//;
my @f; push @f, $+[0] ? $+[0]-1 : 0 while $g =~ /(1)/g; @f
}

my @array = qw (A B C);

print sort { length($a) <=> length($b) }
map "@array[ bts($_) ]\n",
(0 .. 2 ** @array - 1);

...

(surely this can be written more compact,
but that'd take some time ;-)

But better install the Combinatorics
Modules in your private directory
and include them via 'use lib ...'

Regards

M.
 
J

John Bokma

Since I don't have "Math::Combinatorics" installed (nor do I have
permission to install it), I am wondering if there is an alternative.

If you can make a Perl program work, and your own module work there isn't
a reason why you can't install a CPAN module locally.
 
X

xhoster

Since I don't have "Math::Combinatorics" installed (nor do I have
permission to install it),

You don't need permissions, you can install it in user-space. Or you coud
just get the source code from CPAN and copy and paste it.
I am wondering if there is an alternative.

sub foo {
return [@_] unless @_>1;
my $x=shift;
return map {[$x,@$_],[@$_]} foo(@_);
};

use Data::Dumper;
print Dumper [foo(qw/a b c d/)];

Xho
 
D

Dr.Ruud

(e-mail address removed) schreef:
I am looking for an algorithm to do as follows:

my @array = qw (A B C); # This array may have several parameters, for
ex. A B C D

I will like to generate following (not sure if this will be called
permutation):

<NULL>
C
B
A
B C
A C
A B
A B C

Partial solution:

perl -wle 'printf "%0*b\n", 0+@ARGV, $_ for 0..2**@ARGV-1' a b c d
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,661
Latest member
sxarexu

Latest Threads

Top