[Tutor] UselessPython 2.0
Dick Moores
rdm at rcblue.com
Sun Apr 10 07:18:09 CEST 2005
Sean Steeg wrote at 11:22 4/7/2005:
>So we're requesting that anyone with
>one-offs, snippets, mini-apps, full-fledged apps and the like make a
>submission to the new and improved UselessPython. The code doesn't
>have to be pretty, but it does have to work.
I'm a newbie, but would this qualify as a contribution to UselessPython 2.0?
====================================
#isPalindrome.py
# revision of http://www.uselesspython.com/glpalindrome.py to handle
palindromes
# such as "Radar" and "A man, a plan, a canal, Panama!"
# http://www.palindromelist.com/ has many palindromes
# string.ascii_lowercase is 'abcdefghijklmnopqrstuvwxyz'
import string
def isPalindrome(w):
return w == '' or (w[0]==w[-1]) and isPalindrome(w[1:-1]) # recursive
def makeStringAllLowercaseAlpha(s):
"""
Take any string, convert all uppercase alphabetic characters to
lower case,
then strip all non-alphabetic characters
"""
s1 = string.lower(userstring)
s2 = ""
for index in range(len(s1)):
if s1[index] in string.ascii_lowercase:
s2 += s1[index]
return s2
userstring = raw_input('Enter a word or sentence to test: ')
userstringRevised = makeStringAllLowercaseAlpha(userstring)
if isPalindrome(userstringRevised):
print userstring, "is a palindrome."
else:
print userstring, "is not a palindrome."
====================================
Dick Moores
rdm at rcblue.com
More information about the Tutor
mailing list