Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
If the only thing about the order of your permutations that matters, is keeping the number of 0's as low as possible for as long as possible, then another way to accomplish your goal would be to create a complete set of permutations in any order, and then sort the set of permutations according to the number of 0's in each.

For example (using @a...@e):

for my $a (0..@a) { for my $b (0..@b) { for my $c (0..@c) { for my $d (0..@d) { for my $e (0..@e) { # Build a hash with the permutation as the key, and the # number of zeros as the value $permutation{$a[$a].$b[$b].$c[$c].$d[$d].$e[$e]} = !$a + !$b + !$c + !$d + !$e; }}}}} # Now sort according to the number of zeros return sort {$permutation{$a} <=> $permutation{$b}} keys %permutation;
If you want more order than just "least number of zeros first," then you can create a more complex sorting routine using an || operator to break the tie between equal numbers of zeros. For example, to sort first according to number of zeros, and then asciibetical on the permutation created, use this:
return sort {$permutation{$a} <=> $permutation{$b} || $a cmp $b} keys %permutation;
This code doesn't treat @c any differently than the rest of the arrays. If you don't care about @c, you could simply leave it out of the "number of zeros" calculation when assigning things into %permutation.

I hope this helps.

Goshdarnit they both beat me to it, and with nearly the same answer... :)

Alan


In reply to RE: How can I improve this? by ferrency
in thread How can I improve this? by Dogg

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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (7)
    As of 2014-12-28 13:53 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (181 votes), past polls