Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Exercise 6.3. A palindrome is a word that is spelled the same backward and forward, like “noon”
- and “redivider”. Recursively, a word is a palindrome if the first and last letters are the same and the
- middle is a palindrome.
- my first attempt
- """
- def is_palindrome(word):
- return check_palindrome(word, index_first = 0, index_last = (len(word)-1))
- def check_palindrome(word, index_first, index_last):
- if index_last < index_first:
- return True
- print('index_first: {:d}-{:s} --- index_last: {:d}-{:s}' .format(index_first, word[index_first], index_last, word[index_last]))
- if word[index_first] == word[index_last]:
- index_first += 1
- index_last -= 1
- return check_palindrome(word, index_first, index_last)
- else:
- return False
- print(is_palindrome('')) #True
- print(is_palindrome('j')) #True
- print(is_palindrome('kk')) #True
- print(is_palindrome('noon')) #True
- print(is_palindrome('abc')) #False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement