views:

454

answers:

7

Is it a good idea to check for odd/even length of a palindrome number/string? Most snippets I came across don't do this basic test. If length is even, it can't be a palindrome, no?

if len(var) % 2 != 0:
  # could be a palindrome, continue...
else:
  break

Or is it just better (i.e faster) to start comparing the first and last numbers/letters directly?

Edit: Okay, stupid question, should've thought twice! :)

+5  A: 

baab = palindrome and has length of 4 which is even

Aly
4 is the only exception?
Nimbuz
no alternatively we could have baaaab - this is still a palindrome
Aly
**bb** is also a palindrome :-)
Nick D
"b" is also a palindrome. "" is also a palindrome. Sheesh.
John Machin
@John, "b" has odd length. Sheesh.
Nick D
@Nick: Rewording: a palindrome can have any length (even 0).
John Machin
Palindrome does not require even length, it just needs to read the same in both directions. Sheesh. http://en.wikipedia.org/wiki/Palindrome
mynameiscoffey
@mynameiscoffey, that's why they also called crab inscriptions. Sheesh. :o)
Nick D
+1  A: 

Even length strings can be palindromes too. Wikipedia doesn't say anything about this restriction.

Attila Oláh
+8  A: 

ABBA

TabbyCool
Ah! okay, should've thought hard! :)
Nimbuz
Also consider that if you repeat any word forward then backward, whether it has even or odd length, the combined word is going to have an even length, e.g. hello = helloolleh.
Andrew Noyes
I would have never guessed this could be a valid answer to a programming related question.
James Brooks
@James "Dave Jarvis" modified the answer and placed a link, the original answer was quite meaningful.
Nimbuz
Haha, my original answer formed an actual sentence, what have you done?!? Nice link!
TabbyCool
+8  A: 

The easiest way to check for a palindrome is to simply compare the string against it's reverse:

def ispalindrome(s):
   return s == s[::-1]

This uses extended slices with a negative step to walk backwards through s and get the reverse.

sth
did you want two equal signs there (for a Boolean return values)? `return s == s[::-1]`
tgray
use == not = (more characters to make stack overflow happy)
kramthegram
@tgray: oh, yes I did.
sth
wow, and I wrote a 10 line function for it!! :)
Nimbuz
@Nimbuz: know your tools ;-)
Mef
+2  A: 

Simple case: aa.

More complicated case: aaaa.

And so on.

Andrew Noyes
Whenever I read "aa" I get a knee-jerk reaction to say "pahoehoe".
Andrew Dalke
+3  A: 

Try this:

is_palindrome = lambda s : all(s1==s2 for s1,s2 in zip(s[:len(s)/2],s[-1:-(len(s)+1)/2:-1]))

only checks the front half with the back half, and short-circuits as soon as a mismatch is found.

Paul McGuire
This solution takes as much memory as @sth:'s much simpler implementation, is about 1/10th the speed, and says that "ab" is a palindrome. Here's a better solution with only fixed memory overhead: all(s[i]==s[-i-1] for i in range(len(s)//2)) . It's also faster than your example by about 20%.
Andrew Dalke
Ouch! My testing was a little *too* light - I've edited my answer to now fail on testing "ab". My bias lately has been more for iterating over elements than for indexing using integer subscripts, I think I'll take a slightly different run at this using a generator that walks in from both ends of the string.
Paul McGuire
Try islice, izip and reversed? all(c1==c2 for c1,c2 in islice(izip(s, reversed(s)), 0, len(s)//2))
Andrew Dalke
Nice!!!!!!!!!!!!!!!!!
Paul McGuire
A: 

*n=raw_input("Enter a string==>")

n=int(n)

start=0

term=n

while n>0:

result=n%10

start=start*10+result

n=n/10

print start

if term==start: [link text][1] print "True"

else:

print "False"

[1]: http://rahimanpolindrom for srrings.com

RAHIMAN