in reply to Sum of N elements in an M element array

This might help?
There are duplicates, eg 12 and 21.
#!/usr/bin/perl use strict; use warnings; my @perms = glob "{1,2,3}" x 3; print "@perms\n"; @perms = glob "{1,2,3}" x 2; print "@perms\n"; __END__ Prints: 111 112 113 121 122 123 131 132 133 211 212 213 221 222 223 231 232 23 +3 311 312 313 321 322 323 331 332 333 11 12 13 21 22 23 31 32 33
Another run:
#!/usr/bin/perl use strict; use warnings; my @perms = glob "{a,b,c,d,e}" x 3; print "@perms\n"; print "\n"; @perms = glob "{a,b,c,d,e}" x 2; print "@perms\n"; __END__ Prints: aaa aab aac aad aae aba abb abc abd abe aca acb acc acd ace ada adb ad +c add ade aea aeb aec aed aee baa bab bac bad bae bba bbb bbc bbd bbe + bca bcb bcc bcd bce bda bdb bdc bdd bde bea beb bec bed bee caa cab +cac cad cae cba cbb cbc cbd cbe cca ccb ccc ccd cce cda cdb cdc cdd c +de cea ceb cec ced cee daa dab dac dad dae dba dbb dbc dbd dbe dca dc +b dcc dcd dce dda ddb ddc ddd dde dea deb dec ded dee eaa eab eac ead + eae eba ebb ebc ebd ebe eca ecb ecc ecd ece eda edb edc edd ede eea +eeb eec eed eee aa ab ac ad ae ba bb bc bd be ca cb cc cd ce da db dc dd de ea eb ec e +d ee

Replies are listed 'Best First'.
Re^2: Sum of N elements in an M element array
by LanX (Saint) on Feb 03, 2020 at 01:21 UTC
    For the record: your output is significantly different from others, because you allow single elements to appear multiple times, like in "aaa".

    The OP said "repetitions allowed", but I read this as the numbers don't need to be pairwise different.

    To increase the confusion does his demo not show any repetitions.

    >

    ... all possible 3 elements ; a+b+c a+b+d a+b+e ...

    slightly annoying.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      Ok, I wanted to show the glob combination gizmo and the spec wasn't perfect.
      For permutations 3 of a set of 5, consider the following.
      No element is repeated, meaning no element of the input array appears more than once per "sum"
      I just sorted the output in my program editor rather than adding that feature to the Perl code.
      #!/usr/bin/perl use strict; use warnings; use Algorithm::Permute; my $p = Algorithm::Permute->new(['a','b','c','d','e'], 3); while (my @res = $p->next) { print join("+", @res), "\n"; } __END__ # I just used my editor to sort the output # adapt the above code as you wish a+b+c a+b+d a+b+e a+c+b a+c+d a+c+e a+d+b a+d+c a+d+e a+e+b a+e+c a+e+d b+a+c b+a+d b+a+e b+c+a b+c+d b+c+e b+d+a b+d+c b+d+e b+e+a b+e+c b+e+d c+a+b c+a+d c+a+e c+b+a c+b+d c+b+e c+d+a c+d+b c+d+e c+e+a c+e+b c+e+d d+a+b d+a+c d+a+e d+b+a d+b+c d+b+e d+c+a d+c+b d+c+e d+e+a d+e+b d+e+c e+a+b e+a+c e+a+d e+b+a e+b+c e+b+d e+c+a e+c+b e+c+d e+d+a e+d+b e+d+c

        Whilst it is true that no element is repeated, for the purposes of the OP, this solution will produce multiple results for the same three elements being summed in a different order. Some form of filtering, perhaps involving a numerical sort of element numbers, a pack to a string and a grep with a %seen hash, would be required to remove duplicates.

        Update: Here's some code to show what I mean. All the faff with pack q{N*}, @{ $_ } is because we may be dealing with larger arrays where subscripts go into multiple digits. This code:-

        use 5.026; use warnings; use Algorithm::Permute; my @arr = ( 1, 2, 3, 4, 5 ); my $perm = Algorithm::Permute->new( [ 0 .. $#arr ], 3 ); my @allPerms; while ( my @res = $perm->next() ) { push @allPerms, [ sort { $a <=> $b } @res ]; } my @uniqPerms = do { my %seen; map { [ unpack q{N*}, $_ ] } grep { ! $seen{ $_ } ++ } sort map { pack q{N*}, @{ $_ } } @allPerms }; say join q{+}, @{ $_ } for @uniqPerms;

        Produces:-

        0+1+2 0+1+3 0+1+4 0+2+3 0+2+4 0+3+4 1+2+3 1+2+4 1+3+4 2+3+4

        Cheers,

        JohnGG

Re^2: Sum of N elements in an M element array
by abhay180 (Sexton) on Feb 02, 2020 at 08:02 UTC
    Thanks Marshall. This helps. Never a great user of glob() function , will try it out.
      This glob thing is a bit weird.
      Good Luck!
      Consider also:
      #!/usr/bin/perl use strict; use warnings; my @combos = glob "{a,b,c}{1,2}"; print "@combos\n"; __END__ Prints: a1 a2 b1 b2 c1 c2