def precedes(w1, w2):
if w1.count() > w2.count():
return True
elif w1.count() < w2.count():
return False
elif w1.word() < w2.word():
True
else:
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.
2 comments:
I wonder if the following code is easier to reason about:
def precedes(w1, w2):
return (w1.count > w2.count) or \
(w1.count == w2.count and w1.word < w2.word)
SKD: Absolutely right! I wrote that code with the thought of how I might lead students through some simple steps to think about a secondary key in a sort but I appreciate your reminder about what's now being called "Boolean Zen" by some. (No Wikipedia article yet, I see.)
Post a Comment