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:
If the input string x is too short (0-2 characters long), print out: 'Sorry, try something longer.'
Else, if x IS a palindrome, print out: 'YES, "x" is a palindrome.', where x is the input string.
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.>>>
Your program should be roughly structured as follows:
Get user input exp using input().
Build exp_rev, which is reverse of exp. (We practiced a string reversing routine in today's class.)
If exp is two characters or shorter, print out "Sorry, ...".
Else, if exp is the same as exp_rev, print out "YES, ... is a palindrome".
Else, print out "NO, ... is not a palindrome".
When developing a structured program, make sure to proceed step-by-step, verifying intermediate results on the way. Also, don't do all your coding in the script window -- make sure to utilize the SHELL side for testing. If you are unclear what this means, watch the "Rookie vs. Pro way of programming" video again.
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.>>>
You now need three string variables:
exp: this is the user-typed input. You need to keep this around so that you can use it in the response line. Examples: "Penguin", "Madam, I'm Adam."
exp_clean: this is a variant of exp which has been cleaned up. The string has been lowercased and punctuation and space are removed. Examples: "penguin", "madamimadam". Exercise 1 had a question on this, with "It's a GIRL!".
exp_rev: this is exp_clean reversed. Examples: "niugnep", "madamimadam"
To test for palindromehood, you should see if exp_clean and exp_rev are identical. For "Madam, I'm Adam." they are, but for "Penguin" they are not.
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.>>>
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.