Created
August 11, 2012 13:14
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode 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) |
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
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