On Fri, Apr 8, 2016 at 8:08 AM, Chris Angelico <[email protected]> wrote:
> On Fri, Apr 8, 2016 at 11:31 PM, Antoon Pardon
> <[email protected]> wrote:
>> Doing it as follows:
>> seq1 < seq2
>> seq2 < seq1
>>
>> takes about 110 seconds.
>>
>>
>> Doing it like this:
>> delta = cmp(seq1, seq2)
>> delta < 0
>> delta > 0
>>
>> takes about 50 seconds.
>
> Why are you comparing in both directions, though? cmp() is more
> equivalent to this:
>
> 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?
--
https://mail.python.org/mailman/listinfo/python-list