# Generalize Problem : Generate all possible combinations

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
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 possible
combinations.

Thanks,

- raj

## Re: Generalize Problem : Generate all possible combinations

rajsena911@gmail.com (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

## Re: Generalize Problem : Generate all possible combinations

rajsena911@gmail.com (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],
);

## Re: Generalize Problem : Generate all possible combinations

tiltonj@erols.com (Jay Tilton) writes:
>     my @a = (
>         [1, 2, 3],
>         [4, 5],
>         [6, 7],
>     );

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;

\$" = "";
\$, = "\n";
print @l2;
}
print "\n";

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