Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ian Kelly Newsgroups: comp.lang.python Subject: Re: how to convert code that uses cmp to python3 Date: Fri, 8 Apr 2016 08:22:46 -0600 Lines: 40 Message-ID: References: <57064D0D.1030701@rece.vub.ac.be> <5706C961.2000009@rece.vub.ac.be> <57075F43.7060004@rece.vub.ac.be> <85fuuw5ypl.fsf@benfinney.id.au> <5707B2CE.1010407@rece.vub.ac.be> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de sa8mabXUkF6Is4+snkFykgQ3gYtRD48bNmLl3W5D+euA== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'received:209.85.223': 0.03; 'elif': 0.04; 'none:': 0.05; 'paths': 0.05; 'subject:code': 0.07; 'python': 0.10; 'def': 0.13; 'result.': 0.15; '2016': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'seconds.': 0.16; 'subject:python3': 0.16; 'wrote:': 0.16; 'comparing': 0.18; 'delta': 0.22; 'am,': 0.23; 'this:': 0.23; 'header:In-Reply-To:1': 0.24; 'chris': 0.26; 'compare': 0.27; 'equivalent': 0.27; 'fri,': 0.27; 'message- id:@mail.gmail.com': 0.27; 'node': 0.29; 'subject:that': 0.29; 'raise': 0.29; 'code': 0.30; 'version,': 0.30; 'implement': 0.32; 'received:google.com': 0.35; 'follows:': 0.35; 'there': 0.36; 'received:209.85': 0.36; 'possible': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'doing': 0.38; 'received:209': 0.38; 'why': 0.39; 'takes': 0.39; 'to:addr:python.org': 0.40; 'more': 0.63; 'directions,': 0.84; 'pardon': 0.84; 'to:name:python': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to; bh=ytx4HkXxQuvi00tXeGmPodVGn45XOMAt562LJSgDy6I=; b=Ao29bfscxuWhLznq4IYMCJRXz75goSvoAp+B2L/qZ0R9O/nE2Rhm2a14BNq/WDSatD 3gY+7uudN//bfV9Imttdn5yp5S5DkGKuu9dWJ3yHrwft070A/TqDAj0y0jVBCeMUvh0E Dru3esC7hzdic9atKucCOLk3OsgMgMU5hB344j8QrGQPQyQsdsbjbhhyUScC0N7bheoj 8ax2ITbnqlI8HuFdWO+ZmBfe9zAis/xGhaL9H+AsSMR+4JZzoTaaNhcWSntpAeqXg/fc gwQ9rlSHCU7FWVJG+DSAj7I7hy9N/rWdN6PDQb8cAXonhlzpCZOdAyiaVqntZ5XO2KEG 74Yw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to; bh=ytx4HkXxQuvi00tXeGmPodVGn45XOMAt562LJSgDy6I=; b=IkdNvjEoA7/q7edn/mfWpiKN25YxAEcecrAkAuqhZ+wHJe4zA+0vP/TY/A4g1tgg9J IX3eqf3QmG/XODPhtz3rr5yASRUdc2TcsQyXx95uPx+qrDXe6G/JIKV5s6tEoVespHow 2TKu9iKsxFkXxvHKEGxoRWDi2fDIO7Qcwi2lL70462LPvdELsilTCD31TDRLzpvO4fDV GMfUAgZfj4jL/Vwm0hilfn4BCMdWWCoJiIBOpLwlh2Ra3S1XRLtNosddbk6uyRcYYHKD /RkhxdEcu4zj+yO5RnwZaUkJrwMGkTW4BPiPe5zUdzqYeZ9PVjET6jQvbK+X9kWejQm9 H5jw== X-Gm-Message-State: AD7BkJIlk1+44B8anKsaFitmWivLiutAtNdpUaIbXLDFbebI7KUwqJf5AMK77meHGKNoeH14dlnlXb2eStPu8g== X-Received: by 10.107.10.87 with SMTP id u84mr9380418ioi.188.1460125406332; Fri, 08 Apr 2016 07:23:26 -0700 (PDT) In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: <57064D0D.1030701@rece.vub.ac.be> <5706C961.2000009@rece.vub.ac.be> <57075F43.7060004@rece.vub.ac.be> <85fuuw5ypl.fsf@benfinney.id.au> <5707B2CE.1010407@rece.vub.ac.be> Xref: csiph.com comp.lang.python:106685 On Fri, Apr 8, 2016 at 8:08 AM, Chris Angelico wrote: > On Fri, Apr 8, 2016 at 11:31 PM, Antoon Pardon > 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?