Go to: LING 1330/2330 home page  

Palindrome

This one is going to be all about writing a script.
What is your favorite palindrome? 'kayak'? 'repaper'? How about "Don't nod"? The goal of this exercise is to write a Python script that takes a string input from the keyboard and then reports whether or not it is a palindrome. We will do this in four stages, refining the script and introducing enhancements through each step.

Script 1: Naive Palindrome

Your script, named pal_naive.py, takes a user input and reports whether or not it is a palindrome, following this scheme:
  1. If the input string x is too short (0-2 characters long), print out: 'Sorry, try something longer.'
  2. Else, if x IS a palindrome, print out: 'YES, "x" is a palindrome.', where x is the input string.
  3. Otherwise, if x is NOT a palindrome, print out: 'NO, "x" is not a palindrome.'
Note that a palindrome test can be administered by turning a word (e.g., 'cat') into its reverse (e.g., 'tac') and then comparing the two. For now, don't worry about case variation ("Level" is rejected but that's ok), white space ("my gym" will be rejected), or punctuation ("Don't nod" is rejected). We will deal with them in the next stage. Getting the overall structure right is the main focus here.

When executed, the program should work exactly like below:

 
================================ RESTART ===================
Give me a palindrome: aa
Sorry, try something longer.
>>> 
================================ RESTART ===================
Give me a palindrome: noon
YES, "noon" is a palindrome.
>>> 
================================ RESTART ===================
Give me a palindrome: banana
NO, "banana" is not a palindrome.
>>> 

Script 2: Smart Palindrome

Let's introduce some enhancements. This second version of the script, named pal_smart.py, is smart enough to ignore the following variation:
  • Mixed upper and lower case: "Level" as well as "level" should be accepted as a palindrome.
  • Space: "A Toyota" should be recognized as a palindrome.
  • Punctuation: Allow commas, apostrophes, colons, and periods in the input string. That is, these all should test positive: "Madam, I'm Adam.", "A man, a plan, a canal: Panama."

So, the program should now work like:

 
================================ RESTART ===================
Give me a palindrome: aa
Sorry, try something longer.
>>> 
================================ RESTART ===================
Give me a palindrome: Level
YES, "Level" is a palindrome.
>>> 
================================ RESTART ===================
Give me a palindrome: penguin
NO, "penguin" is not a palindrome.
>>> 
================================ RESTART ===================
Give me a palindrome: Madam, I'm Adam.
YES, "Madam, I'm Adam." is a palindrome.
>>> 

Script 3: Insistent Palindrome

This time, let's make our palindrome script, named pal_loop.py, more demanding. That is, make it loop back until the user has given a palindrome. Start from the last script and build in a looping mechanism.

When a user-supplied string fails to be a palindrome, your script should automatically prompt again for input, which continues until the typed input is finally a palindrome. Additionally, it starts off with a nice greeting and ends with a proper goodbye. It should work like this:

 
================================ RESTART ===================
Hello! Let's start.
Give me a palindrome: k
Sorry, try something longer.
Give me a palindrome: elephant
NO, "elephant" is not a palindrome. Let's try again.
Give me a palindrome: penguin
NO, "penguin" is not a palindrome. Let's try again.
Give me a palindrome: No lemon, no melon
YES, "No lemon, no melon" is a palindrome.
Goodbye.
>>> 

Want more excitement? Try this page, and this.

Script 4: Modular Palindrome

Let's put finishing touches to our palindrome script, neatly encapsulating some key routines into custom functions. Build two functions: getRev() which reverses a given string, and cleanInput() which removes punctuation ans whitespace from the input string.