Palindrome

pal.py: 5 points


Readme

You have two weeks to work on Lab 7, rather than just one week. We recommend pacing your work so that you are done with both Part 1 (pal.py) and Part 2 (ask.py) by the end of Week 1.

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.

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.

Your program must use recursion, and you may not use loops. You 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.

Example Output

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