adventures in optimization

The charming adventures of an analyst and his solver.

« Friday, April 1, 2011 »

Reformed JAPHs in Python - Alphabetic Indexing

Python obfuscation.

Full disclosure: I used to be a Perl programmer. (Hey, Jeff.)

One day I became disillusioned at the progress of Perl 6 and decided to import this. This appears to be a fairly common story for Perl to Python converts. While I haven't looked back much, there are a number of things I really miss about perl (lower case intentional). Among other things, I miss having value types in a dynamic language, magical and ill-advised use of cryptocontext, and sometimes even pseudohashes because they were inexcusably weird. A language that supports so many ideas out of the box enables an extended learning curve that lasts for many years. "Perl itself is the game."

Most of all I think I miss writing Perl poetry and JAPHs. Stupidly, I didn't keep any of those I wrote, and I'm not competent enough with the language anymore to write interesting ones. At the time I was intentionally distancing myself from a model that was largely implicit and based on understanding of archaic system internals and moving to one that was (supposedly) explicit and simple. After switching to Python as my primary language, I used the following email signature in a nod to this change in worldview (intended for Python 2.x):

print 'just another python hacker'

Recently I've been experimenting with writing JAPHs in Python. I think of these as reformed JAPHs. They accomplish the same purpose as programming exercises but exist with a far more restricted context. In some ways they are more challenging. Creativity is difficult when functioning within a more explicit landscape. I now have a small series of reformed JAPHs dedicated to a close friend which increase monotonically in complexity. Here is the first one, written in plain understandable Python 3.2:

import string

letters = string.ascii_lowercase + ' '
indices = [
     9, 20, 18, 19, 26,  0, 13, 14, 19, 7,  4, 17, 26,
    15, 24, 19,  7, 14, 13, 26,  7,  0, 2, 10,  4, 17

print(''.join(letters[i] for i in indices))

This is fairly simple. Instead of explicitly embedding the string 'just another python hacker' in the program, we assemble it using the index of its letters in the string 'abcdefghijklmnopqrstuvwxyz '. Obfuscation is achieved through a series of minor measures:

  • Instead of calling the print function, we import sys and make a call to sys.stdout.write.
  • string.lowercase + ' ' is assembled by joining together the character versions of its respective ordinal values (97 to 123 and 32).
  • The integer indices are joined together using 'l' and split into a list.
  • Liberal use of ''' and the fact that multiple subsequent strings in Python are concatenated.

Here's the obfuscated version:

).write(''.join(map(lambda _:(list(map(chr,range(97,123)))+[chr(

More could certainly be done, but that's about as far as this one captivated my interest. Stay tuned for the next JAPH.