This series of questions by this Anonymous Monk has the uncanny sound of someone who hasn't applied much discipline in deriving a proper method. It's delved into the realm of 'voodoo programming' where special cases are added on top of previous hacks to ensure that page 3 gets the right 20 elements. No offense or anything, but when you're going in circles, it helps to reconsider the technique.

Whenever I get flummoxed with problems involving iteration or geometry, I whip out the graph paper and stay away from the keyboard until I'm certain of the correct approach. Keyboards force you to think in terms of the symbols, which is not always the most effective way to think.

Draw a diagram of the database's records. Draw little lines or brackets showing the region you want for a given query. If real-world parameters like '20 elements' is too cumbersome to draw in the diagram, use smaller parameters like 4 or 5. (This trick will also help you properly parameterize your solution in code later.)

Once you're sure you've identified all the parameters, simulate the query on another sheet with a fresh diagram of the database. Be the computer. Be ruthlessly naive and follow only the steps you've properly and fully explained. If you can't empathize with your student, you can't teach the student.

Then you're ready to type in the pseudocode in English. Write your pseudocode as a series of comments. In plain English. With little text versions of your diagrams if that helps. In just plain English without variable names at all. Explain what you want done before you make the computer do it. Did I mention you should write in plain English? Strategy in comments.

Finally, translate the pseudocode into real code underneath each sentence. Explain in computer language exactly how they should accomplish the goal. Use the parameters you've identified, and use the steps that will work. Tactics in code.

The proper code will have an obvious reason for each statement and variable, and you won't need special cases or hacks or adjustments for any weird artifacts that aren't a crucial part of your problem definition. Someone else has a .sig which reads something like "I know I'm on the right track when I'm deleting code in order to provide more functionality."

--
[ e d @ h a l l e y . c c ]


In reply to Re: mathematical equation by halley
in thread mathematical equation 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.