Palindromes (3.5 points)¶
A palindrome word is a
word that reads the same backwards as forwards, i.e., that reads the
same when reversed. Examples: 'rotor' and 'noon' are
palindrome words.
Write function palindromes() that takes a list of
words (str) and returns another list with
those words in the given list that are palindromes and have an odd
number of letters. The returned list must be sorted
lexicographically.
Hint: A possible solution uses the fact that the reverse of a string s can be obtained with the expression s[::-1]
Save this function in file palindromes.py.
Examples:
>>> lw1 = ['mom', 'car', 'wheel', 'peep', 'wow', 'rotator', 'guitar', 'piano']
>>> palindromes(lw1)
['mom', 'rotator', 'wow']
>>> lw2 = ['civic', 'tree', 'noon', 'nine', 'deified', 'book', 'hannah', 'pen']
>>> palindromes(lw2)
['civic', 'deified']
>>> lw3 = ['nun', 'peep', 'table', 'stats', 'redder', 'light', 'level', 'radar', 'sheet', 'otto', 'pullup', 'bag']
>>> palindromes(lw3)
['level', 'nun', 'radar', 'stats']
Note
More tests are provided in file test-palindromes.txt.