Advertisement
uopspop

Untitled

Oct 17th, 2018
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.99 KB | None | 0 0
  1. """
  2. Exercise 6.3. A palindrome is a word that is spelled the same backward and forward, like “noon”
  3. and “redivider”. Recursively, a word is a palindrome if the first and last letters are the same and the
  4. middle is a palindrome.
  5.  
  6. my first attempt
  7.  
  8. """
  9.  
  10. def is_palindrome(word):
  11.     return check_palindrome(word, index_first = 0, index_last = (len(word)-1))
  12.  
  13. def check_palindrome(word, index_first, index_last):
  14.     if index_last < index_first:
  15.         return True
  16.    
  17.     print('index_first: {:d}-{:s} --- index_last: {:d}-{:s}' .format(index_first, word[index_first], index_last, word[index_last]))
  18.     if word[index_first] == word[index_last]:
  19.         index_first += 1
  20.         index_last -= 1
  21.         return check_palindrome(word, index_first, index_last)
  22.     else:            
  23.         return False        
  24.    
  25. print(is_palindrome('')) #True
  26. print(is_palindrome('j')) #True
  27. print(is_palindrome('kk')) #True
  28. print(is_palindrome('noon')) #True
  29. print(is_palindrome('abc')) #False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement