Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.musoftware.de!wum.musoftware.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Neil Cerutti Newsgroups: comp.lang.python Subject: Re: bit count or bit set && Python3 Date: 25 Oct 2012 20:00:25 GMT Organization: Norwich University Lines: 31 Message-ID: References: <5089511E.4090009@earthlink.net> <50896152$0$29978$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Trace: individual.net DA6Enl24BgBzB3HU59FopgCL5SEWkyV2Btwy2w6XHYLN4Z0ZXuxjScBz3pRCs6xq67 Cancel-Lock: sha1:zF5nifpZrUALN7y5b9mtl++qFbo= User-Agent: slrn/0.9.9p1/mm/ao (Win32) Xref: csiph.com comp.lang.python:32156 On 2012-10-25, Steven D'Aprano wrote: > On Fri, 26 Oct 2012 02:31:53 +1100, Chris Angelico wrote: >> On Fri, Oct 26, 2012 at 2:25 AM, Christian Heimes >> >> wrote: >>> Simple, easy, faster than a Python loop but not very elegant: >>> >>> bin(number).count("1") >> >> Unlikely to be fast. > > Oh I don't know about that. Yes indeed! Python string operations are fast enough and its arithmetic slow enough that I no longer assume I can beat a neat lexicographical solution. Try defeating the following with arithmetic: def is_palindrom(n): s = str(n) return s = s[::-1] > Here's some timing results using Python 2.7: Excellent work. You can of course drop to C for arithmetic and likely triumph over Python strings. That's never been applicable for me, though. -- Neil Cerutti