palindrome iteration
Arnaud Delobelle
arnodel at googlemail.com
Sun Aug 29 06:36:57 EDT 2010
Matteo Landi <landimatte at gmail.com> writes:
> Well, I tried the also the solution posted above (recursive w/o
> slicing and iterative), and I discovered they were the slowest..
>
> is_palindrome_recursive 2.68151649808
> is_palindrome_slice 0.44510699381
> is_palindrome_list 1.93861944217
> is_palindrome_reversed 3.28969831976
> is_palindrome_recursive_no_slicing 6.78929775328
> is_palindrome_iterative 4.88826141315
What are the last two functions?
I suggest another:
def is_palindrome(s):
return all(map(str.__eq__, s, reversed(s)))
:)
> Nothing to say about the iterative function, but the benchmark of the
> recursive was unexpected, at least for my point of view: do you think
> it is due to the try/except overhead?
>
> On Sun, Aug 29, 2010 at 8:53 AM, Josh English
> <joshua.r.english at gmail.com> wrote:
>> This whole conversation got interesting, so I thought I'd run some
>> speed tests:
>>
>> The code:
>> from timeit import Timer
>>
>> def is_palindrome_recursive(s):
>> if len(s) <= 1:
>> return True
>> if s[0] != s[-1]:
>> return False
>> else:
>> return is_palindrome(s[1:-1])
This should be return is_palindrome_recursive(s[1:-1]). If this is
copy-pasted, then you may call a different is_palindrome function and
invalidate the timings!
[...]
--
Arnaud
More information about the Python-list
mailing list