adventures in optimization

The charming adventures of an analyst and his solver.

« Monday, April 11, 2011 »

Reformed JAPHs in Python - Rolling Effect

Python obfuscation with a cute visual effect.

Here's a JAPH composed solely for effect. For each letter in 'just another python hacker' it loops over each the characters ' abcdefghijklmnopqrstuvwxyz', printing each. Between characters it pauses for 0.05 seconds, backing up and moving on to the next if it hasn't reached the desired one yet. This achieves a sort of rolling effect by which the final string laboriously appears on our screen.

import string
import sys
import time

letters = ' ' + string.ascii_lowercase
for l in 'just another python hacker':
    for x in letters:
        print(x, end='')

        if x == l:
            print('\b', end='')


Locating and printing of each letter in the string is done via a list comprehension. At the end we actually have an extra line of code (the eval statement) that gives us our newline.

[[(lambda x,l:str(print(x,end=''))+str(__import__(print.
__doc__[print.__doc__.index('stdout') - 4:print.__doc__.
index('stdout')-1]).stdout.flush()) + str(__import__(''.
'\b',end='\x09'.strip())if x!=l else'*&#'))(x1,l1)for x1
.index(l1)+1]]for l1 in'''just another python hacker''']
eval('''\x20\x09eval("\x20\x09eval('\x20 print()')")''')