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 second attempt:
- new concept: 'abc'[-100,300] is a valid syntax. And we get an empty string
- """
- def is_palindrome(word):
- """ empty string is a palindrome so is a character """
- if len(word) <= 1:
- return True
- if word[0] == word[-1]:
- return is_palindrome(middle(word))
- else:
- return False
- def middle(word):
- """Returns all but the first and last characters of a string."""
- return word[1:-1]
- print(': ' + str(is_palindrome(''))) #True
- print('j: ' + str(is_palindrome('j'))) #True
- print('kk: ' + str(is_palindrome('kk'))) #True
- print('noon: ' + str(is_palindrome('noon'))) #True
- print('abc: ' + str(is_palindrome('abc'))) #False
- print('redivider: ' + str(is_palindrome('redivider'))) #True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement