NextPermute() and NextPermuteNum() modify the input array in place, and (to traverse all permutations) require that the input starts off sorted. So minimally adapting your previous code to use Algorithm::Loops would look (untested) something like this:

#!/usr/bin/env perl use strict; use warnings; use Algorithm::Loops qw(NextPermuteNum); use Data::Dump; use feature qw(say); my @n = sort { $a <=> $b } ( 1, 1, 1, 2, 3, 3, 3, 4, 5, 5, 5, 6, 7, 8, + 11, 12, 13, 14, 14, 15 ); my $m3 = qr/(.)\1\1/; my $m2 = qr/(.)\1/; do { my $s = pack( "(A*)*", @n ); if ( $s =~ /($m3)/ ) { say $1; sleep 1; goto PERMUTE; } if ( $s =~ /($m2)/ ) { goto PERMUTE; say $1; sleep 1; } say join " ", @n; PERMUTE: ; # empty statement to attach the label to } while (NextPermuteNum( @n )); __END__

As for the 15!/8! figure: imagine each of the 1s is a different colour, then there are 15! distinguishable ways to arrange the numbers. Take any one of those (valid solution or not) and it has 8 coloured 1s in some order; and each of the 8! ways to order those colours will appear once in what is otherwise the same arrangement of numbers.


In reply to Re^9: Algorithm RFC: fast (pseudo-)random shuffle with no repetition by hv
in thread Algorithm RFC: fast (pseudo-)random shuffle with no repetition by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.