Palindromes and Python: Let me count the ways

I was looking for some simple Python code examples for my four programming kids.  We are going to do a little graphics so they can see Pygame then do some string and number manipulation.  Just some tinkering, nothing super fancy.  Offhand I could not think of anything so as usual I started thinking of something else.

The other day I was subbing for the golf coach and the kids were done with the math assignment in about five minutes.  Most of the class started on other assignments or went to reading.  One boy had his head on his book bag ready to take a nap.  Jokingly I said instead of taking a nap why doesn’t he find out how many palindromes there are from 1 to 1000.  To my surprise he sits up and asked what a palindrome was.  (My surprise was not that he did not know what a palindrome was, but the fact that he sat up and asked.)  I explained what a palindrome was. He gets a piece of paper and starts figuring.  He was not writing them down one-by-one, but was looking for a pattern.  (I have no idea how many palindromes there are from 1 to 1000.  Suppose if I am going to pose these types of things I should know the answer.  Python project.)  Anyway, from there I mentioned Project Euler.  He made an account and started in.  That led me back to Project Euler.  Might be a good place to find some simple Python projects to fiddle with for the class.  Here is problem number 4.

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.

Find the largest palindrome made from the product of two 3-digit numbers.

That looks like a good Python type project.  I Google “Python palindrome”.  OMG.  Do you know how many ways there are to write a function to identify palindromes?  Many.  Iterative, recursive, built in function, all with their own subset of variations.  I have code snippets up the wazoo, all solving the same problem.  What more could a programming teacher ask for?!  Be still my heart.  Admittedly, this approach may not work with every programing class (Math?  Yuck.) but with some classes and some kids it is a strategy.

Most of the Python palindrome functions are using the Pythonese text manipulation functions involving slicing; colons and brackets.  I do not remember any of these.  It is going to be the “here a book, here is some code, figure this out, tell me what is going on” method of teaching code.  I like it.  I probably ought to figure out all that colon and bracket business again.

For the right students Project Euler is a gold mine for programming ideas.  For other students it would be the end of their interest in programming (Math?  Yuck.).  Finding the right balance and interests is the key.



2 Responses to “Palindromes and Python: Let me count the ways”

  1. gasstationwithoutpumps Says:

    Project Euler does have some cool programming projects for a mathy kid, but the palindromes from 1 to 1000 is quicker to do in your head: 9 1-digit ones, 9 2-digit ones, 9*10 3-digit ones for a total of 108 palindromes.

  2. gflint Says:

    That is where he was going then I proposed from 1 to n. I sure hope I get this kid in my CS class next year.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: