Checking a Number for Palindromic Behavior
MRAB
python at mrabarnett.plus.com
Tue Oct 20 11:39:50 EDT 2009
Benjamin Middaugh wrote:
> Actually I was working on a program to test the so-called 196-algorithm
> as an extracurricular activity. MRAB was most helpful with pointing out
> what I should have already thought of. My previous attempts were
> hampered by my limited knowledge of python, and I had already mentally
> committed to my complex and inefficient method of solving the problem
> (big mistake, I know). My final solution, with suitable adjustments for
> proper ways of doing things :-), was just 20 lines of code.
>
> By the way, I don't get much time for python since I'm a freshman
> computer engineering student taking 17 credits. My Intro to Programming
> class uses C++ (yuck) instead of python, so I have to devote most of my
> programming time to that. However, I figured python would be a better
> choice for trying this algorithm out.
>
> Thanks for all the help. As a beginning programmer, I'm really
> appreciating it.
>
BTW, if you're working in a lower-level language like C or C++ and
already have the number as a string then testing for a palindrome is
quicker if you work from both ends of the string, comparing the digits,
until they meet or cross in the middle.
In a higher-level language like Python, reversing and comparing is
faster.
Different languages have different strengths and weaknesses and may
require different approaches.
More information about the Python-list
mailing list