Generalize Problem : Generate all possible combinations

R

rajsena

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

A. Sinan Unur

(e-mail address removed) (rajsena) wrote in @posting.google.com:
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
 
J

Jay Tilton

(e-mail address 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]}>;
 
A

Arndt Jonasson

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 "".)
 

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
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top