[Tutor] Code for checking whether an input string is a palindrome or not.
Danny Yoo
dyoo at hkn.eecs.berkeley.edu
Fri Oct 13 19:04:35 CEST 2006
>> Yes, you are right; the code isnt working for 'abcdba';
>> So what should I do to rectify it.
>
> Try walking through the code by hand so you understand why it is
> failing. Then maybe you can figure out how to fix it. It's important to
> learn to find and fix errors yourself.
Hi Asrar,
On a slight tangent: you have a bunch of code that takes input from the
user, and then checks it for palindromeness. If you have a
palindrome-checking function that eats strings, then you can do tests
without pestering the user.
So my suggestion is to try to take your existing code and make a function
called 'is_palindrome' (or if you want, something shorter like pali()).
The key is that once you have a function, you should be able to write
small tests like:
def pali(word):
"""Returns True if word is palindromic."""
## ... fill me in
## Test cases:
assert (pali("aa") == True)
assert (pali("abcdba") == False)
Once you have something like this, then you can run all tests together
every time you change the palindrome function. As you've experienced, the
problem is subtle enough that there's going to be a few edge cases. You
want to make it easy to make sure all the things that worked before
continue to work.
More information about the Tutor
mailing list