1

I try to write a function for palindrome.below is my code

def ispalindrome(string): if len(string) <= 1: return True else: if string[0] == string[-1]: return True else: return False 

Only one thing I don't understand is one of my results it says "ispalindrome('123321') should return False", and my result for this one is true. I don't know what to do now. Anyone can help me correct this?

2

1 Answer 1

1

You just check the first and the last element, you didn't check the characther in the middle

def ispalindrome(x): if len(x) <= 1: return True else: n = len(x) for i in range(n//2): if x[i] != x[n-1-i]: return False return True 

Also, "123321" is indeed a palindrome.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.