Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
This falls somewhere between a meditation and a tutorial. It's about using the NestedLoops function from Algorithm::Loops, but it's also about a certain mental model I have found useful recently.

A normal set of nested loops is like the odometer on a car. The outermost loop is the leftmost digit, and the innermost loop is the rightmost digit. A normal odometer can only loop through the same set of digits on each dial, but in programming, each dial can have a completely different set of values.

Now imagine that each time one of the dials changes, all the dials to the right of it can be remade. For example, each dial could exclude any numbers that exist on any dial to its left. A whole set of dials like that would generate the permuations of a set of numbers. In any situation where you're generating a set of values, and you can build them member-by-member, you can probably use this odometer model. And NestedLoops makes it pretty easy. All you need to mess with is the first argument.

The first argument to NestedLoops is the set (arrayref) of dials it will iterate over. A dial can be either an arrayref or a coderef that, when executed, returns an arrayref. So you can generate dials dynamically. Furthermore, when the coderef is executed, @_ will contain the values that are currently on the dials to the left, so you can do the kind of filtering I talked about above. With a little bookkeeping, I was able to make an elegant derangement iterator the other day.

Today, find all paths of length n in a graph inspired yet another solution in the same vein. Simply keeping track of where you can go next, you can explore a graph. And for the OP in that thread, while you're filtering, you can check to see if the set of characters you're assembling is a prefix of any valid words, to save a whole lot of checking later.

So keep your eyes open for solutions like this. They are easy and efficient.


Caution: Contents may have been coded under pressure.

In reply to NestedLoops and the Odometer Model by Roy Johnson

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 chanting in the Monastery: (5)
    As of 2014-12-22 10:58 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

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





      Results (116 votes), past polls