I thought they reached you. Here they are again: def palindrome(str, i=0, j=-1): try: if str[i] == str[j]: return palindrome(str, i + 1, j - 1) return False except IndexError: return True
def palindrome(str, i=0, j=-1): try: while True: if str[i] != str[j]: return False i, j = i + 1, j - 1 return True except IndexError: return True On Sun, Aug 29, 2010 at 12:36 PM, Arnaud Delobelle <arno...@googlemail.com> wrote: > Matteo Landi <landima...@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.engl...@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 > -- > http://mail.python.org/mailman/listinfo/python-list > -- Matteo Landi http://www.matteolandi.net/ -- http://mail.python.org/mailman/listinfo/python-list