The apprentice was deeply disturbed: “Master, your answers are counter to all of the math I have ever learned!” “This is how mathematics is performed in my dojo,” the master retorted. The apprentice pleaded, “But master, can I be warned if I perform an operation that results in an unknowable answer?” “Of course,” said the master, and taught his apprentice an ancient incantation.

Some time later the apprentice was preparing to add some large numbers. He cautiously uttered the incantation. Nothing seemed to happen, so he proceeded with his mathematical operations. Suddenly his master appeared. Without a word, he simultaneously drew his sword and struck a match. He then proceeded to commit harakiri while burning his dojo to the ground. As the apprentice fled for his life, he was enlightened.

]]>This first one really puzzled me for a while and made me question the basic foundations of my Python knowledge, but it turns out that the solution is quite reasonable. I was given the following piece of code, which should print out a multiplication table, and told to fix the bug that was causing it to malfunction. See if you can spot it:

table = [[0]*10]*10 for i in range(1,11): for j in range(1, 11): table[i-1][j-1] = i*j for i in range(10): for j in range(10): print table[i][j], print

Now because I’m not Dutch (see `import this`

), I was pulling my hair out for a while.
I even resorted to verifying that Python was parsing the code in the way I expected.
This task is made surprisingly easy by the ast module.
I eventually did come to my senses though after looking a little more closely.

In Python, mutable objects like lists are never copied unless you specifically ask for copying.
The first line above looks like it’s making a list of ten sublists, each containing ten zeroes.
In reality though, it makes a list of ten *references* to the same one sublist with the ten zeroes.
Note that this issue doesen’t affect the actual zero values themselves as they are immutable.
To demonstrate:

>>> table[0] is table[1] True >>> from itertools import combinations >>> all(r1 is r2 for (r1, r2) in combinations(table, 2)) True

So all of our rows are actually stored in the same list. Because of this, whenever we update a value in any row of our table it effectively updates all of the rows, putting that value in the same position. There is really only one row, it just happens to be accessible in ten different places. That’s why we end up with all of the rows having the values we’d expect to be in the last row.

Here’s a better way to print out the multiplication table, by simply printing the values instead of storing them and reading them back.
It also uses `xrange()`

instead of `range()`

to save some memory and aligns all of the columns with string formatting.

for i in xrange(1, 11): for j in xrange(1, 11): print "%3d" % (i * j), print

Stay tuned for more coding solutions and Python trivia!

]]>Instead, I’ll simply say that I greatly appreciate the wisdom I’ve gained from others who contribute to sites similar to this one. There have been countless times when I’ve been searching for a solution to a problem so specific I thought I’d never find an answer. But then I find that some kind soul has run into the exact same dilemma and has taken the time to share their solution with the world. Hopefully through this blog I’ll be able to share my own solutions to esoteric problems and help others avoid the situation depicted below.

*Wisdom of the Ancients by Randall Munroe / CC BY-NC 2.5*

Either that or it’ll be something pretty for potential employers to admire.

]]>