Guest User

Untitled

a guest
Jan 8th, 2012
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.70 KB | None | 0 0
  1. def is_palin_iterative_from_recursive(s):
  2.     """
  3.    I created this version by transforming the recursive solution to
  4.    an iterative one. In my tests its a bit faster then `is_plain_strrec`
  5.    """
  6.     s  = str(s)
  7.     while True:
  8.         if not s:
  9.             return True
  10.         elif s[0] != s[-1]:
  11.             return False
  12.         else:
  13.             s = s[1:-1]
  14.  
  15. def is_palin_iterative_with_while(s):
  16.     """
  17.    Like the iterative version, but uses a while loop
  18.    and thus avoids some of the overhead in the for-loop version
  19.    """
  20.     s  = str(s)
  21.     i = 0
  22.     t = len(s) // 2
  23.     while i < t:
  24.         if s[i] != s[-(1+i)]:
  25.             return False
  26.         i += 1
  27.     return True
Advertisement
Add Comment
Please, Sign In to add comment