Wednesday, November 8, 2017

If you can't find a disc, it ain't where you're looking

There's a disc golf adage that goes "If you can't find a disc*, it ain't where you're looking." I sometimes quote that to students who can't find a bug—they're often convinced that a bug lies in code that is in fact correct. I saw an interesting case of that just now with a Python function I wrote:
def precedes(w1, w2):
    if w1.count() > w2.count():
        return True
    elif w1.count() < w2.count():
        return False
    elif w1.word() < w2.word():
        return False

precedes(w1,w2) should return True if w1 (a word) has a count that's greater than w2. Ties are resolved in favor of the word that is lexicograhically less.

I found that tie resolution wasn't working. I convinced myself that I must have gotten a w1 / w2 reversed, or a conditional operator backwards. I looked and looked at those three comparisons but just couldn't find anything wrong with them.

*Golf discs are about 8.5" in diameter and are often made of bright-colored plastic.