On 2012-10-25, Steven D'Aprano <steve+comp.lang.pyt...@pearwood.info> wrote: > On Fri, 26 Oct 2012 02:31:53 +1100, Chris Angelico wrote: >> On Fri, Oct 26, 2012 at 2:25 AM, Christian Heimes >> <christ...@python.org> >> 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 -- http://mail.python.org/mailman/listinfo/python-list