Wednesday, June 16, 2004

Exercise[29]

Give not that which is holy unto the dogs, neither cast ye your pearls before swine, lest they trample them under their feet, and turn again and rend you.
Matthew 7:6
King James Bible

The study of algorithms offers much to the practicing programmer. A course on the subject equips students with algorithms for important tasks and design techniques for attacking new problems. We'll see in later columns how advanced algorithmic tools sometimes have a substantial impact on software systems, both in reduced development time and faster execution speed.

As crucial as those sophisticated ideas are, algorithms have a more important impact at a more common level of programming. In his book Aha! Insight (from which I shamelessly stole my title), Martin Gardner describes the contribution I have in mind: "A problem that seems difficult may have a simple, unexpected solution." Unlike the advanced methods, the aha! insights of algorithms don't come only after extensive study; they're available to any programmer willing to think seriously before, during and after coding.

Jon Bently
“Column 2: Aha! Algorithms”, p. 11
Programming Pearls, 1986

Professor Strunk was a positive man. His book contained rules of grammar phrased as direct orders. In the main I have not attempted to soften his commands, or modify his pronouncements, or delete the special objects of his scorn.
E. B. White
“A Note on This Book”, p. v
The Elements of Style, 1959

It turns out that style matters in programming for the same reason that it matters in writing. It makes for better reading.
Douglas Crockford
“Chapter 9 - Style”, p. 94
JavaScript: The Good Parts, 2008

Functional composition is better.
Email message from Douglas Crockford
Sent to John Spurgeon on Sun, Oct 23, 2016 at 7:53 PM

Tom Finnegan's Big Problem

Tom Finnegan has a homework assignment that is due soon. He's been procrastinating and hasn't been paying attention in class, so he copied some clever code that Tamsin Finnegan wrote. Tom thought he was being clever until Tamsin informed him, at the eleventh hour, that the code he copied wasn't code for Tom's assignment at all. It was just something she did for fun! (Tamsin was wise to Tom all along and is hanging him out to dry.) Tom's actual assignment is to implement a Utility method called josephusNumber, which must be defined in terms of two parameters: rebelCount and skipCount. The method must return the Josephus number corresponding to the argument values passed to the method as demonstrated here. (Note that skipCount may be zero or negative!) Tom's teacher gave him some "Flippant Functions" to work with. All he has to do is find the right function and modify it.

Your Assignment

Using Tom's Flippant Functions, help Tom get out of his jam.

The function Tom needs to find is named cycleShiftLeft and is defined in terms of three parameters: n, i and j. After you've found the function, make sure you understand its purpose and how it works. Then figure out what needs to change. Make the necessary changes and test them. After you have tested your function using the Test Fixture below:

  1. Copy your modified code directly from the Test Fixture below and paste it into the body of an email message.
  2. If you want to receive the bonus points, include your answer to the bonus question in the body of the email message.
  3. Make sure the message is addressed to: jspurgeon@vcstudent.org
  4. Make sure the subject of the message is: Exercise[29]
  5. Send the email.

Grading Rubric

  • Not counting bonus points, this assignment is worth 100 points.
  • If your submission is late or if you submit a corrected assignment after the due date, you will lose 10 points.
  • If your code does not produce the correct results, you will lose up to 50 points.
  • If the style of the code that you submit is not consistent with the style of the code provided, you may lose up to 10 points.
  • If your code is not submitted in the body of an email message as instructed above, you will lose 10 points.
  • If your code is not sent to the correct email address, you will lose 10 points.
  • If the subject of your email message is not "Exercise[29]", you will lose 10 points.
  • If you answer the bonus question (below) correctly, you will receive up to 10 bonus points.

Test Fixture

Bonus Question (worth 10 points)

What's the connection between this exercise and the parable of the laborers? (Matthew 20:1-16)

Due Date

EXTENDED: This assignment is due by end of day Tuesday, 29 Nov 2016.