On Sat, Apr 9, 2016 at 12:22 AM, Ian Kelly <ian.g.ke...@gmail.com> wrote: >> seq1 == seq2 >> seq1 < seq2 >> >> You only need ONE comparison, and the other is presumed to be its >> opposite. When, in the Python 3 version, would you need to compare >> twice? > > When there are three possible code paths depending on the result. > > def search(key, node): > if node is None: > raise KeyError(key) > if key < node.key: > return search(key, node.left) > elif key == node.key: > return node > else: > return search(key, node.right) > > How would you implement this with only one comparison?
I was assuming that the equality check could be a lot cheaper than the inequality, which is often the case when it is false (it's pretty easy to prove that two enormous objects are different - any point of difference proves it). Doing the equality check first generally means you're paying the price of one expensive lookup each time. If proving that x != y is expensive too, then call it two comparisons rather than three. But you still don't need to check both directions of inequality. ChrisA -- https://mail.python.org/mailman/listinfo/python-list