Coding Education STEM

Let’s Play Palindrome! (Python)

Did you know that the longest Palindromic word in the Oxford English Dictionary is “tattarrattat” coined by James Joyce in Ulysses (1922) meaning “a knock on the door”?

What is a Palindrome, you ask? Well, you already know it or have seen it. According to Wikipedia:
“A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward or forward. Allowances may be made for adjustments to capital letters, punctuation, and word dividers.”

So, “Amma” is same spelled backward “ammA” (remember, capital letters don’t matter). So, it’s a Palindrome. “ABBa” is too. “111” is too. “123” is not.

How would you test if a string (again, letters or numbers) if a Palindrome through your own function/code? Here I’ll show you (with explanation, not just for memorization or copy/paste) how to do it in C, and Python…the idea is the same for both. Only syntax varies.

Logic:

The idea simply is to compare the string (word or a number), and compare it to its reversed version. Reversing “bob” gives us “bob”, so it’s a Palindrome. Reversing “bobs” gives us “sbob” so obviously it’s NOT a Palindrome.

Here’s the pseudocode in Python:

  • get input from user: using input() function and store it in a variable (e.g. userinput)
  • Reverse it using userinput[::-1]  # this seems like a weird syntax but that’s Python! The :: says copy each char of the whole thing, and  -1 says “starting backward”, essentially reversing the original string to a new variable on the left side and store it in another variable (e.g. rev)
  • Compare the original userinput with the reversed variable rev. If they’re equal, it’s a Palindrome, other it is not.

Okay, but what if the entry was blank (a space, or just Enter)? Or just one character such as “1” or “a”…of course, reverse of the same would be the same! I don’t like that at all, so I check for those by checking the length of the input using len() function…so here’s the additional logic:

  • if the string is empty/blank or only 1 character, then don’t check, tell user that it should be more than that; otherwise do the above procedure.

To make it cleaner, we can make 2 helper functions and call them from the main body of code…so that main body code would get the input and show the output to the user, and it’d call a function to reverse the input, and another function to check if it’s a Palindrome.

So, the reverse helper function can be:

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

And the helper function to check if the string passed to it is a Palindrome or not is called isPalindrome and returns True or False and is defined as:

def isPalindrome(s): 
    rev = reverse(s) # call the helper fn
# Checking if both string are equal or not 
if (s == rev): # too easy in Python!
     return True
return False # otherwise

We are done. So, to tie it all altogether, 1) get the input 2) if the string is not at least 2 characters, show a message and quit 3) otherwise, call the helper functions. The code now is this:

# Main code entry...
# get a string/word from input
userinput = input("Enter a word to check if it is a Palindrome: ")
if len(userinput) <2:
print("Input must be 2+ characters.")
exit(0);

# Pass the input and call the fns to get result:
ans = isPalindrome(userinput)

if ans == 1: 
        print("Yes") 
else: 
        print("No")

So, go ahead and try your strings! Numbers or letters and it’ll ALWAYS give you the right answer.

If you want to see the same implementation in C language, check out the blog here.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top