Solving the web most popular code shortening competition with Python 3

Alessandro Amici - @alexamici - <a.amici@bopen.eu>

B-Open, Rome - http://bopen.eu (we're hiring!)

Abstract

“Code shortening” is the “sport” where participants strive to achieve the shortest possible source code that solves a programming problem by exploiting all the tricks and quirks of the language.

The SIZECON problem on SPOJ is one of the oldest and most popular code shortening problems on the web with a bizarre twist, only character above ASCII value 32 are counted for the penalty. During the talk we will take a journey into some frightening depths of the Python language in order to write shorter and shorter solutions to SIZECON until, exploiting a number of mind-blowing tricks, we will reach the current record solution for Python 3 of 31 characters.

I promise I’ll show you the most obfuscated, contrived and sick python code you have ever seen and (hopefully!) will ever see. I invite participants to give SIZECON a try and check their score against the Python3 rankings.

SPOJ and SIZECON

SPOJ is a coding platform and an online judge:

  • support for 45+ languages
  • a huge trove of 20.000+ problems
  • 50.000+ users
  • user scores are public -> ranks
  • solutions are not public -> can compete any time

SIZECON is a unusual "code golf" problem:

  • created in 2005
  • top20 most popular problems on SPOJ with 8000+ users including all languages, 1400+ for python
  • our own Tim Peters is among the very best solvers, in Perl

SIZECON problem statement

SIZECON best solutions for Python 3

The "python golf" master plan

  • reference solution
    • correctness
  • ​alternative algorithms​
    • algorithm wizardry
  • shortened solutions
    • language wizardry

Reference solution

Go to the Jupyter notebook ->

Alternative algorithms

Nothing to see here*,

please move along.

* as long as you forget the bizarre ASCII special characters exception.

Shortened solutions

Go to the Jupyter notebook ->

Child's play

Alternative algorithms

We can use as many ASCII special characters as we like...

Code legal characters

Alternative algorithms

We can use as many ASCII special characters as we like...

String literal legal characters

Alternative algorithms

Algorithm building blocks:

  1. a string literal with lots of ASCII special characters
  2. turn the string literal into source code
  3. run the source code
exec(ASCII32_decrypt('{encrypted_source_code}'))
exec('{source_code}')
ASCII32_encrypt('{source_code}')) -> '{encrypted_source_code}'

Building ASCII32_decrypt

What we want

  • ​it get as input a string full with ASCII control characters

  • it outputs a string of python code

  • il must be short!

str.translate(table)
Return a copy of the string where all characters [...] have been mapped through the given translation table, which must be a string of length 256.
exec("{encrypted_source_code}".translate("{decrypt_table}"))

ASCII32_decrypt translate flavour

Go to the Jupyter notebook ->

"SIZECON2" problem

Same problem as SIZECON, but the score is the number of "different" characters with ASCII > 32.

Go to the Jupyter notebook ->

"Downward is the only way forward"

Go to the Jupyter notebook ->

"That many exec's within exec's is too unstable."

Python interpreter: decrypts the ASCII32_encrypt'ed string

exec level 1: decrypts the number_encrypt'ed string

exec level 2: finally runs the solution

The SIZECON 31-solution

Go to the Jupyter notebook ->

SIZECON absolute best solutions!

"Your condescension, as always, is

much appreciated, thank you."

 

Alessandro Amici

<a.amici@bopen.eu>

@alexamici

http://linkedin.com/in/alexamici

 

B-Open, Rome - http://bopen.eu

We are hiring!

Solving the web most popular code shortening competition with Python 3 - PyCon.it 2017

By Alessandro Amici

Solving the web most popular code shortening competition with Python 3 - PyCon.it 2017

“Code shortening” is the “sport” where participants strive to achieve the shortest possible source code that solves a programming problem by exploiting all the tricks and quirks of the language. The SIZECON problem on SPOJ is one of the oldest and most popular code shortening problems on the web with a bizarre twist, only character above ASCII value 32 are counted for the penalty. During the talk we will take a journey into some frightening depths of the Python language in order to write shorter and shorter solutions to SIZECON until, exploiting a number of mind-blowing tricks, we will reach the current record solution for Python 3 of 31 characters. I promise I’ll show you the most obfuscated, contrived and sick python code you have ever seen and (hopefully!) will ever see.

  • 1,238