Generalize Problem : Generate all possible combinations

Discussion in 'Perl Misc' started by rajsena, Nov 8, 2004.

  1. rajsena

    rajsena Guest

    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
    rajsena, Nov 8, 2004
    #1
    1. Advertising

  2. (rajsena) wrote in news:b15e70aa.0411080947.66cf4073
    @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
    A. Sinan Unur, Nov 8, 2004
    #2
    1. Advertising

  3. rajsena

    Jay Tilton Guest

    (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]}>;
    Jay Tilton, Nov 9, 2004
    #3
  4. (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 "".)
    Arndt Jonasson, Nov 10, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?iso-8859-1?q?Thomas_K=F6llmann?=

    How to "generalize" a function?

    =?iso-8859-1?q?Thomas_K=F6llmann?=, Apr 24, 2005, in forum: Python
    Replies:
    7
    Views:
    324
    =?iso-8859-1?q?Thomas_K=F6llmann?=
    Apr 28, 2005
  2. rbt

    all possible combinations

    rbt, Jul 13, 2005, in forum: Python
    Replies:
    36
    Views:
    21,399
    Steve Holden
    Jul 28, 2005
  3. Nash
    Replies:
    7
    Views:
    324
  4. Nash
    Replies:
    6
    Views:
    341
  5. John

    function to generalize

    John, Oct 9, 2003, in forum: Javascript
    Replies:
    1
    Views:
    74
    Richard Cornford
    Oct 9, 2003
Loading...

Share This Page