Image of the glider from the Game of Life by John Conway
Skip to content

Number Arrays in Python

Being new to Python and completely discovering the language has been quite the experience. I find it realy cool that it is whitespace dependant, and that indenting matters. This helps reduce alot of the "chatter" that is common in C/C++, Java, Perl, etc.

This week, I decided, among other things, to write a game in Python. A game called Su Doku. I got hooked on it over the weekend, and I have done almost 100 puzzles in the past few days. So I thought it would be cool to program the game. Heck, what an exercise, eh?

First things first, write the pseudocode. I figure the steps for creating such a game would be as follows:

  • Only the numbers 1 through 9 are possible
  • Create the "rules" of the puzzle in that no number can be in the same row, column or 3x3 quadrant
  • Build the 9x9 array using the "rules" established
  • Create "patterns" using 25-30 of the numbers that make up the solution
  • Show only the pattern of numbers to the user
  • Check the users solution against the 9x9 array solution for accuracy

Fairly straight forward. I am sure that there are probably some steps missing, and no doubt it can get more detailed, but the skeleton is there. So, I begin setting up the array using the standard list array in the Python language. It comes as a surprise to me, however, that Python treats every element in the list array as strings. If any integer, long or float arithmetic is done on that element, the element is converted to a number, and the manipulation is done "under the hood". In other words, speed is compromised, and there is no numeric array built into the Python core.

Luckily, for Python developers, the language supports modules, much like the import statement in Java, so numeric arrays are supported. However, I did find it a little frustrating that it is not supported in the core. Although the math will not be complex, and in my situation, small, speed won't be an issue. But still, it's the principle that matters.


{ 1 } Comments