Velocity Reviews > Perl > Generalize Problem : Generate all possible combinations

# Generalize Problem : Generate all possible combinations

rajsena
Guest
Posts: n/a

 11-08-2004
Hi,

I would really appreciate if somebody could help me with this problem

Problem: Need to generate All possible combinations for a N-bit number
wherein each position as a finite (1-6) possibilities.

Heres a simple example

Array1 { 1 2 3 }
Array2 { 4 5 }
Array3 { 6 7 }

Generate all possible combinations such that,

Number 3 bit

1 bit comes from array1
2 bit comes from array2
3 bit comes from array3

possible combinations

1 4 6
1 4 7
1 5 6
1 5 7
2 4 6
2 4 7 and so on....

I need to generalize this for a N bit number (determined at runtime)
where in each position is comes from an array of {1-6} possible
combinations.

Thanks,

- raj

A. Sinan Unur
Guest
Posts: n/a

 11-08-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (rajsena) wrote in news:b15e70aa.0411080947.66cf4073

> Hi,
>
> I would really appreciate if somebody could help me with this problem
>
> Problem: Need to generate All possible combinations for a N-bit number
> wherein each position as a finite (1-6) possibilities.

We are not going to solve your homework and/or job interview question.

However, you should be able to solve your problem by using the appropriate
module from CPAN (http://search.cpan.org/). 'Permutation' might be a
suitable keyword to use.

Once you have attempted a solution on your own and have code, please come
back and post specific issues you are having. However, before doing that, I
would recommend reading the posting guidelines for this group. The
guidelines are posted here regularly.

Sinan

Jay Tilton
Guest
Posts: n/a

 11-09-2004
(E-Mail Removed) (rajsena) wrote:

: I would really appreciate if somebody could help me with this problem

Help in what way?

: Problem: Need to generate All possible combinations for a N-bit number
: wherein each position as a finite (1-6) possibilities.
:
: Heres a simple example
:
: Array1 { 1 2 3 }
: Array2 { 4 5 }
: Array3 { 6 7 }

[...]

: possible combinations
:
: 1 4 6
: 1 4 7
: 1 5 6
: 1 5 7
: 2 4 6
: 2 4 7 and so on....

my @a = (
[1, 2, 3],
[4, 5],
[6, 7],
);
local(\$,,\$")="\n";print<@{[map{local\$"=',';"{@\$_}"}@a]}>;

Arndt Jonasson
Guest
Posts: n/a

 11-10-2004

(E-Mail Removed) (Jay Tilton) writes:
> my @a = (
> [1, 2, 3],
> [4, 5],
> [6, 7],
> );
> local(\$,,\$")="\n";print<@{[map{local\$"=',';"{@\$_}"}@a]}>;

Nice. Here is a broken up and rearranged version, so that the ones
confused by the one-liner can work out for themselves how it works:

my @a = (
[1, 2, 3],
[4, 5],
[6, 7],
);
{
local(\$,,\$");

\$" = ",";
my @l1 = map {"{@\$_}"} @a;

\$" = "";
my @l2 = <@l1>;
\$, = "\n";
print @l2;
}
print "\n";

(My perl 5.005 didn't like \$" being undef, so I set it to "".)