Skip to content

Instantly share code, notes, and snippets.

@ffuentese
Created August 1, 2018 21:50
Show Gist options
  • Save ffuentese/d26e3675346f0e39ec5792458dac94af to your computer and use it in GitHub Desktop.
Save ffuentese/d26e3675346f0e39ec5792458dac94af to your computer and use it in GitHub Desktop.
Palindrome in Python
import string
import unicodedata
def reverse_string(str):
# reverses the phrase
return ''.join(reversed(str))
def clean(str):
# helps ignoring punctuation as we check palindrom phrases
translator=str.maketrans('','',string.punctuation)
return str.translate(translator)
def normalize(str):
# removes accents (if present) to compare
text = unicodedata.normalize('NFD', str)
text = text.encode('ascii', 'ignore')
text = text.decode("utf-8")
return text
quit = ""
while(quit.lower() != "q"):
print("Enter the phrase:")
frase = input()
reverso = reverse_string(clean(frase.strip())).replace(" ", "").lower()
reverso = normalize(reverso)
original = clean(frase.strip()).replace(" ", "").lower()
original = normalize(original)
if (original == reverso):
print("The phrase is a palindrome")
else:
print("The phrase is NOT a palindrome")
print("Press Q to quit or any other to continue")
quit = input()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment