Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
One solution that comes to mind is to generate the list out-of-order then sort it into order.
@bar=sort {zc($a) <=> zc($b)} permute([0..2],[0..2],[0..2],[0..2],[0..2]); sub zc{ my $arg=shift; my @a=split //,$arg; return (grep /0/,@a[0..1])+(grep /0/,@a[3..4]); }
This is shorter (lines-of-code wise) than the 16 loops you describe, but will run slower. You could build the same sort of idea into the algorithm by using a heap w/ the same sort rule, but the end result would be algorithmically equivalent performance wise.

Another approach (it is too late tonight for me to work this out completely): Assume that permute($a,$b,$c,$d,$e) takes array references as its arguments and returns a list of all the permutations of the elements of those lists. Also assume that the function nz takes a list reference and returns all non-zero elements of that list. Then what you want to do is call permute like this:

push @list,permute(nz($a),nz($b),$c,nz($d),nz($e)); push @list,permute([0],nz($b),$c,nz($d),nz($e)); push @list,permute(nz($a),[0],$c,nz($d),nz($e)); push @list,permute(nz($a),nz($b),$c,[0],nz($e)); push @list,permute(nz($a),nz($b),$c,nz($d),[0]); push @list,permute([0],[0],$c,nz($d),nz($e)); etc...
Basically the "16 loops" you describe above. There is a very definite pattern to how the arguments of permute are set up for each call. Now all that is left is to figure out how to generate that pattern.

In reply to Re: How can I improve this? by lhoward
in thread How can I improve this? by Dogg

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (8)
    As of 2018-03-19 19:34 GMT
    Find Nodes?
      Voting Booth?
      When I think of a mole I think of:

      Results (246 votes). Check out past polls.