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:
One way of doing it is to 'clean string' (convert all lowercase, remove spaces and punctuation) and only after that make the comparison.
12345678910111213>>>
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?'
)
False
Something along those lines:
1234567891011>>>
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.