http://www.perlmonks.org?node_id=11132353

TryingTheBest has asked for the wisdom of the Perl Monks concerning the following question:

Hi everyone.

I am working with a combination script that is giving me problems.

This is as below:

I have two static numbers: 1 and 2.

And also I have a non-static number that represent the size of the combinations.

Example: 3 (combinations of 3 characters).

I need to perform ALL combinations using the static number, but considering the non-static number.

In the example with "3" as non-static number, this will be:

1 1 1
1 1 2
1 2 1
1 2 2
2 1 1
2 1 2
2 2 1
2 2 2

( I think I don't forget any combination :P )

Someone can help me with the code, please? I tried using use Algorithm::Combinatorics qw( combinations ); but I could not solve it.

Thanks all!

Replies are listed 'Best First'.
Re: Combinations with variable length
by davido (Cardinal) on May 10, 2021 at 22:07 UTC

    Your intuition to use Algorithm::Combinatorics is correct, but your choice of functions within that module was incorrect. The correct function exists, and is documented in the POD: variations_with_repetition.

    use Algorithm::Combinatorics qw(variations_with_repetition); my @data = (1,2); my $at_a_time = 3; my $variations_iterator = variations_with_repetition(\@data, $at_a_tim +e); while (my $variant = $variations_iterator->next) { print "@$variant\n"; }

    This produces:

    1 1 1 1 1 2 1 2 1 1 2 2 2 1 1 2 1 2 2 2 1 2 2 2

    You were so close!

    In the future, please post the sample code. In this case I would have used it to verify that you were using the module correctly, aside from using the incorrect function.

    Also, a lesson from high school math, and from the POD: There are n^k variations with repetition. So in your case the input set size is 2, and you're taking 3 at a time. 2**3=8. So we can validate that we got the correct number of variations by checking that we got 8, and that there are no repeats.


    Dave

Re: Combinations with variable length
by LanX (Cardinal) on May 10, 2021 at 21:43 UTC
    DB<19> $dyn = 3 DB<20> $gen = join " ", ("{1,2}") x $dyn DB<21> say for <"$gen"> 1 1 1 1 1 2 1 2 1 1 2 2 2 1 1 2 1 2 2 2 1 2 2 2

    HTH! :)

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Re: Combinations with variable length
by tybalt89 (Prior) on May 10, 2021 at 21:30 UTC
    #!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11132353 use warnings; my @static = qw( 1 2 ); my $nonstatic = 3; my @item = []; @item = map { my $static = $_; map [ $static, @$_ ], @item } @static for 1 .. $nonstatic; use Data::Dump 'dd'; dd \@item;

    Outputs:

    [ [1, 1, 1], [1, 1, 2], [1, 2, 1], [1, 2, 2], [2, 1, 1], [2, 1, 2], [2, 2, 1], [2, 2, 2], ]
Re: Combinations with variable length
by ikegami (Pope) on May 12, 2021 at 15:55 UTC
    for ( 0 .. 2**$n-1 ) { my @combo = map { $_ + 1 } split //, sprintf("%0*b", $n, $_); say "@combo"; }

    use Algorithm::Loops qw( NestedLoops ); my $iter = NestedLoops([ map [ 1, 2 ], 1..$n ]); while ( my @combo = $iter->() ) { say "@combo"; }

    Seeking work! You can reach me at ikegami@adaelis.com

Re: Combinations with variable length
by jo37 (Hermit) on May 11, 2021 at 18:47 UTC

    Just to make sure: You aren't trying to cheat on PWC 112, are you?

    Greetings,
    -jo

    $gryYup$d0ylprbpriprrYpkJl2xyl~rzg??P~5lp2hyl0p$
Re: Combinations with variable length
by Anonymous Monk on May 10, 2021 at 22:14 UTC
    $amount = 3; @pick = (1,2); @end = (); for($i=0;$i<$amount;$i++) { for($z=0;$z<2**($amount-1-$i);$z++) { for($j=0;$j<@pick;$j++) { for($k=0;$k<2**$i;$k++) { push(@{$end[$i]},$pick[$j]); } } } } for($z=0;$z<2**($amount);$z++) { for($i=0;$i<$amount;$i++) { print $end[$i][$z]."\t"; } print "\n"; }