bit count or bit set && Python3

Neil Cerutti neilc at norwich.edu
Fri Oct 26 08:56:27 EDT 2012


On 2012-10-25, Ian Kelly <ian.g.kelly at gmail.com> wrote:
> On Thu, Oct 25, 2012 at 2:00 PM, Neil Cerutti
> <neilc at norwich.edu> wrote:
>> 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]
>
> Problems like these are fundamentally string problems, not math
> problems.  The question being asked isn't about some essential
> property of the number,  but about its digital representation.
> Certainly they can be reasoned about mathematically, but the
> fact remains that the math being done is about the properties
> of strings.

The "unexpected" part, to me, is that an optimal arithmetic based
solution conceptually is more efficient. You need to compute just
half the digits of the number and then perform a contant compare
operation.

-- 
Neil Cerutti



More information about the Python-list mailing list