pal.py: 5 points
The goal of this part of the lab is to write a program that, given a string from the user, recursively determines whether or not it is a palindrome. A palindrome is any string that reads the same forward as backwards. “UFO tofu” and “No, sir, away! A papaya war is on!” are both examples of palindromes. (For this exercise, we ignore whitespace, punctuation and capitalization.)
Within pal.py
, you should write a recursive function is_pal(s)
that returns the Boolean True
if s
is a palindrome and False
otherwise. We can recursively check whether s
is a palindrome as follows: if s
has length 0 or 1, then it is a palindrome. Otherwise, it is only a palindrome if the first character is the same as the last character, and the substring with those two characters removed is also a palindrome. If the first character is different from the last character, then you know it is not a palindrome.
You must use recursion to implement is_pal
. As you write this function, please note the following two things are not allowed in order to get full credit:
recursive_reverse
from the Warmup as part of solving this functionYou may assume s
has been pre-processed, i.e. you can assume that all spaces and other non-letters have been removed, and all characters are lowercase. So the second example given above would actually have been input as “nosirawayapapayawarison”. Make sure your code is commented.
To complete pal.py
, you should include a main()
function which asks the user for a string s
, calls is_pal(s)
on that string, and then prints out whether the string is or is not a palidrome, as shown in the example output below.
Some examples of running python pal.py
are below.
Please enter a string: racecar
"racecar" is a palindrome
Please enter a string: blargh
"blargh" is not is a palindrome
Reminder
As usual, please commit and push your changes before moving on to the rest of the lab.