Zalak Gohil
Zalak Gohil

Reputation: 1

Check a text is palindrome or not after ignoring punctuations

The code which I typed with the help of some online coding website is:

def reverse(text):
    return text[::-1]

def sanitize(text):
    text = text.lower()
    forbidden =("",".",",","!","&","?")
    for c in forbidden:
        text=text.replace(c,"")
    return text

something = raw_input("enter your detail")

def is_palindrome(text):
    text = sanitize(text)
    return text == reverse(text)


if is_palindrome(something):
    print(" yes, it is palindrome")
else:
    print("no, it is not palindrome")

Output:

enter your detail:rise to vote,sir
no, it is not palindrome

What did I do wrong?

Upvotes: 0

Views: 57

Answers (2)

Zalak Gohil
Zalak Gohil

Reputation: 1

def reverse(text): return text[::-1]

def sanitize(text): text = text.lower() forbidden =(" ",".",",","!","&","?") for c in forbidden: text=text.replace(c,"") return text

something = raw_input("enter your detail")

def is_palindrome(text): text = sanitize(text) return text == reverse(text)

if is_palindrome(something): print(" yes, it is palindrome") else: print("no, it is not palindrome")

Upvotes: 0

12944qwerty
12944qwerty

Reputation: 2017

"rise to votesir" is not the same as "risetov ot esir". Maybe replace the spaces too when you sanitize?

This depends on your criteria and how you "sanitize it".

def sanitize(text):
    text = text.lower()
    forbidden =(" ",".",",","!","&","?") # Replaced "" with " " because it was redundant
    for c in forbidden:
        text=text.replace(c,"")
    return text

Upvotes: 2

Related Questions