Take the 2-minute tour ×

I've been working on this Palindrome Python problem for a while now. Here is the prompt:

palindrome(T) is True if T is the same as 
           the backwards version of T, and otherwise
           is False.  

palindromes(L) returns a list of the palindromes in L

>>> palindrome("madam")
>>> palindrome([6,9,6])
>>> palindrome("T r ")
>>> palindromes( [ [1], [3,2], [5,1,5], [0,0,1], [7,3,7,3] ] )
[[1], [5, 1, 5]]
>>> palindromes( "son daughter dad mom ewe any".split() )
['dad', 'mom', 'ewe']
>>> palindromes( "stressed desserts stop pots live evil".split() )
>>> palindromes( ["stressed desserts", "stop pots", "can can", "too few"] )
['stressed desserts', 'stop pots']

if __name__ == "__main__":
    import doctest

Here is what I have written so far:

   def palindrome(L):
        if L == L[::-1]:
            return True
            return False

    def palindromes(L):
        return filter(palindrome ,range(6))

I am able to get the first four tests when they are simple True/False answers, but when I have to start using the filter function I begin to have trouble. I know that I have to do some sort of indexing, but I'm not exactly sure how.

share|improve this question
Hint: You may want to do something with that L that is passed in to your function. –  Jeff Mercado Oct 13 '11 at 7:09
You also may just return L == L[::-1] in the first function. –  refaim Oct 13 '11 at 7:12
Why 'range(6)'? –  Don Oct 13 '11 at 7:17
add comment

closed as not a real question by casperOne Oct 23 '12 at 12:08


1 Answer

Why did you use range(6)

You should just do:

def palindromes(L):
    return filter(palindrome ,L)

And, as suggested in the comments, you should do:

def palindrome(L):
    return L == L[::-1]

When you use the == is a comparison operator, you will get either true or false so that line is enough.

share|improve this answer
add comment
