CodeKata Two: Karate Chop

Dave's specification: http://blogs.pragprog.com/cgi-bin/pragdave.cgi/Practices/Kata/KataTwo.rdoc

My implementations:

  • Iterative: codekata/2/chop_iterative.py
    • Time spent:
      • Five minutes. (I implemented this one after the recursive and built-in versions.)
    • Errors/issues encountered:
      • Practically none. I surprised the hell out of myself when it passed all tests the first time I ran it. It was really just a matter of converting the built-in tracking of min/max position in the array to an iterative, rather than recursive, control structure. Easy stuff.
    • Inspiration:
      • One of Dave's three standard suggestions.
    • Comments:
      • Doing this after the recursive version was probably cheating; it was far too easy to be considered any kind of "practice". I still consider the recursive version more "elegant", and there appears to be very little difference in execution speed, if the timing reported by the unit tests is any indication (not that you can get very accurate when run speeds are the neighborhood of a twentieth of a second).
  • Recursive: codekata/2/chop_recursive.py
    • Time spent:
      • About an hour of trying to remember my old Data Structures and Algorithms course from 12 years ago (at which I failed miserably, and proceeded to work through it logically), and about 15 minutes of coding/debugging.
    • Errors/issues encountered:
      • My first cut of this didn't use a passed-along offset, but instead did the bookkeeping manually at each level of recursion. Then I remembered that this was Python, and that we can pass state along between recursion depths so easily, it'd be a crime to do it yourself.
    • Inspiration:
      • One of Dave's three standard suggestions.
    • Comments:
      • This is the classic solution to the problem, and I still think of this as the most intuitive of the ways to tackle it. It "just feels right". I noticed a small optimization (an extra else: that wasn't needed) when adapting this to the iterative version, and came back after the fact to clean it up.
  • Built-in: codekata/2/chop_builtin.py
    • Time spent:
      • 1 minute.
    • Errors/issues encountered:
      • None.
    • Inspiration:
      • My first instinct is almost always to turn to language features and standard libraries for an implementation, and to deviate only when it's obvious the standard implementation is lacking in some fashion. In this case, it was an EXACT fit, aside from the calling convention.
    • Comments:
      • You can call this a cop-out until you're blue in the face, but sometimes, the best implementation of something is the one you don't have to write.

Others: