Python Forum
Palindrome in Python - result always false
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Palindrome in Python - result always false
#1
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 Smile )
Regards,
RavCoder
Reply
#2
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
Reply
#3
(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?
Reply
#4
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]
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
(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 Smile
Reply
#7
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.
Reply
#8
(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?
Reply
#9
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!
Reply
#10
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Simple Palindrome Inkanus 4 2,120 Nov-25-2020, 04:11 PM
Last Post: perfringo
  difference between «1 in [2] == False» and «(1 in [2]) == False» fbaldit 2 2,237 Apr-20-2020, 05:39 PM
Last Post: fbaldit
  Post JSON from python to PHP don't give expected result pgagnebin 1 3,748 Sep-04-2019, 10:29 PM
Last Post: micseydel
  python hmac gave different result than php hash_hmac nadavvin 2 3,177 Feb-18-2019, 03:35 PM
Last Post: nadavvin
  python result problem of an iteration algorithm for power allocation Jessica 1 2,632 Sep-07-2018, 08:08 PM
Last Post: micseydel
  python code (embedded in Redshift) to return result of the query Mel 0 2,456 Aug-24-2018, 06:12 PM
Last Post: Mel
  Palindrome mp3909 3 4,237 Oct-18-2017, 01:54 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020