bit count or bit set && Python3
Neil Cerutti
neilc at norwich.edu
Thu Oct 25 16:00:25 EDT 2012
On 2012-10-25, Steven D'Aprano <steve+comp.lang.python at 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
>> <christian at 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
More information about the Python-list
mailing list