Posts: 69
Threads: 20
Joined: Sep 2019
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
Posts: 32
Threads: 1
Joined: Oct 2019
Oct-16-2019, 08:54 AM
(This post was last modified: Oct-16-2019, 08:57 AM by baquerik.)
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
Posts: 69
Threads: 20
Joined: Sep 2019
Oct-16-2019, 09:00 AM
(This post was last modified: Oct-16-2019, 09:00 AM by RavCOder.)
(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?
Posts: 8,159
Threads: 160
Joined: Sep 2016
Oct-16-2019, 09:03 AM
(This post was last modified: Oct-16-2019, 09:03 AM by buran.)
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]
Posts: 8,159
Threads: 160
Joined: Sep 2016
(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
Posts: 69
Threads: 20
Joined: Sep 2019
Oct-16-2019, 09:11 AM
(This post was last modified: Oct-16-2019, 09:11 AM by RavCOder.)
(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
Posts: 1,950
Threads: 8
Joined: Jun 2018
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'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 69
Threads: 20
Joined: Sep 2019
(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?
Posts: 32
Threads: 1
Joined: Oct 2019
Oct-16-2019, 12:21 PM
(This post was last modified: Oct-16-2019, 12:25 PM by baquerik.)
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!
Posts: 1,950
Threads: 8
Joined: Jun 2018
Oct-16-2019, 12:46 PM
(This post was last modified: Oct-16-2019, 12:46 PM by perfringo.)
(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
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
|