Oct-16-2019, 12:46 PM
(Oct-16-2019, 12:40 PM)perfringo Wrote:(Oct-16-2019, 12:18 PM)RavCOder Wrote: I didn't understand exactly.
What do you mean with all cases?
And how should I set my conditions for testing?
You could extend your function so that it returns True for following palindromes:
>>> def is_palindrome(word): ... word_start = '' ... for letter in word: ... word_start = letter + word_start ... if word == word_start: ... return True ... ... return False ... >>> is_palindrome('Hannah') False >>> is_palindrome('Was it a car or a cat I saw?') FalseOne way of doing it is to 'clean string' (convert all lowercase, remove spaces and punctuation) and only after that make the comparison.
Something along those lines:
>>> import string >>> def is_palindrome(text): ... cleaned = [letter.lower() for letter in ''.join(text.split()) if letter not in string.punctuation] ... return all(forward == backward for forward, backward in zip(cleaned, reversed(cleaned))) ... >>> is_palindrome('Was it a car or a cat I saw?') True >>> is_palindrome('Hannah') True >>> is_palindrome('better') False
It's too complex for me what you did, I'm too stupid for that.
Not that your code is bad or anything, it's just me that I don't understand.