Hi,
I'm implemented this function to check if a string is palindrome or not.
But when I use a palindrome word such as "radar" for example, my function return False.
def is_polindrome_word(word):
word_start = ''
for letter in word:
word_start = letter + word_start
if( word == word_start):
return True
return False
test_word= 'radar'
if __name__ == "__main__":
print(is_polindrome_word(test_word))
How can I do return True my word?
and if there are suggestion to make my code better , I will accepted (an explanation would be welcome ,because I'm beginner
)
Regards,
RavCoder
def is_polindrome_word(word):
return word==word[::-1]
test_word= 'radar'
print(is_polindrome_word(test_word))
Output:
True
[::-1] allows you to revert a string
(Oct-16-2019, 08:54 AM)baquerik Wrote: [ -> ]def is_polindrome_word(word):
return word==word[::-1]
test_word= 'radar'
print(is_polindrome_word(test_word))
Output:
True
[::-1] allows you to revert a string
Thanks, if I didn't want to use slice notation instead?
it works for me when indentation is correct
def is_palindrome(word):
word_start = ''
for letter in word:
word_start = letter + word_start
if word == word_start:
return True
return False
for word in ['radar', 'blabla']:
print(f"{word} ==> {is_palindrome(word)}")
Output:
radar ==> True
blabla ==> False
and for improvements, well
def is_palindrome(word):
return word == word[::-1]
(Oct-16-2019, 09:00 AM)RavCOder Wrote: [ -> ]if I didn't want to use slice notation instead?
Is this a homework> if yes - post your restrictions in advance and in the proper forum section
(Oct-16-2019, 09:03 AM)buran Wrote: [ -> ]it works for me when indentation is correct
def is_palindrome(word):
word_start = ''
for letter in word:
word_start = letter + word_start
if word == word_start:
return True
return False
for word in ['radar', 'blabla']:
print(f"{word} ==> {is_palindrome(word)}")
Output:
radar ==> True
blabla ==> False
and for improvements, well
def is_palindrome(word):
return word == word[::-1]
Thanks, now It works, I didn't see that it wasn't indented correctly because my compiler didn't report identation error.
No, it's not a homework, I'm self-taught
Some things to consider:
- name 'Hannah' should be palindrome but with current code it's not
- palindrome can mean different things for different people. According to
wikipedia article:
Quote:A palindrome is a word, number, phrase, or other sequence of characters which reads the same backward as forward, such as taco cat or madam or racecar or the number 10801. Sentence-length palindromes may be written when allowances are made for adjustments to capital letters, punctuation, and word dividers, such as "A man, a plan, a canal, Panama!", "Was it a car or a cat I saw?" or "No 'x' in Nixon".
You can try to write a function which covers all the types of palindromes defined in wikipedia.
(Oct-16-2019, 10:24 AM)perfringo Wrote: [ -> ]Some things to consider:
- name 'Hannah' should be palindrome but with current code it's not
- palindrome can mean different things for different people. According to wikipedia article:
Quote:A palindrome is a word, number, phrase, or other sequence of characters which reads the same backward as forward, such as taco cat or madam or racecar or the number 10801. Sentence-length palindromes may be written when allowances are made for adjustments to capital letters, punctuation, and word dividers, such as "A man, a plan, a canal, Panama!", "Was it a car or a cat I saw?" or "No 'x' in Nixon".
You can try to write a function which covers all the types of palindromes defined in wikipedia.
I didn't understand exactly.
What do you mean with all cases?
And how should I set my conditions for testing?
You could change the code so it ignores symbols and uppercase/lowercase distinctions, so it matches more types of palindromes like:
Hannah
hannah, wow hannah!
(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 strings which can be interpreted as 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?')
False
One 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