Skip to content

Instantly share code, notes, and snippets.

@dgrtwo
Created August 11, 2012 13:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dgrtwo/3324382 to your computer and use it in GitHub Desktop.
Save dgrtwo/3324382 to your computer and use it in GitHub Desktop.
This simple script translates a Python program into another, equivalent, Python program using a minimal set of characters.
"""
Usage: python minimal_Python.py infile outfile
Programs output by this translator will run the same way as the original and
use only the characters:
()+1cehrx
The code is for entertainment only. Converting your code base to minimal Python
is not recommended.
"""
import sys
def convert_txt(txt):
"""Convert the text of a Python program to minimal Python"""
s = "+".join(["chr(%s)" % "+".join(["1"] * ord(c))
for c in txt])
return 'exec(%s)' % s
def convert_file(infile, outfile):
"""Convert a Python file to minimal Python"""
with open(infile) as inf:
txt = inf.read()
with open(outfile, "w") as outf:
outf.write(convert_txt(txt))
if __name__ == "__main__":
[infile, outfile] = sys.argv[1:]
convert_file(infile, outfile)
@vishvananda
Copy link

Nicely done. I was hoping someone could beat 9 characters. That is also the best I've been able to come up with. My solutions:

https://gist.github.com/3325686

@vishvananda
Copy link

found a 7 character solution, but it uses an external file, which may be 'cheating'. Its also at the above gist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment