Background
Minifiers are used, commonly, when serving JavaScript to your Web Browser. It is commonly used to reduce the number of bytes that have to be sent. Saving bandwidth is useful for obvious reasons. Some people use obfuscaters (that intentionally make code harder to read), I am not talking about those.
We will be minifying Python 2
I was debating on whether or not use JavaScript or Python for the minifying experience and I decided on Python for two reasons: white space matters and I think that will add an interesting dynamic the problem. Additionally, using Python 2.7 will provide another dynamic, such as remove superfluous ()
during a print (i.e. print("Hello world")
vs. print"Hello world"
). I would have, personally, preferred to open it up to any language, but for some languages this process won't make a lot of sense for. And, what language you decide to minify will directly impact your score (and if the language even can be minified).
Specs
Your goal is to only modify the code in a way that will not change it's functionality in anyway. You may, of course, change variable names (inside your minifying program) as long as it doesn't effect output (keep track of scope). Although I am giving you a specific program, please don't optimize for the test case as all standard loopholes are forbidden.
Score: length of the program after you minified it.
Input: Any Python 2.7 program (that contains no errors)
Output: A minified version.
Although your code should be able to accomodate all valid Python 2.7 input, it is necessary to test your script against something in order to prove it's effectiveness.
Click here to view the example program.
Making the problem more approachable
Feel free to use or modify any code found inside my solution (listed bellow). I did this to get you started with quote basic quote handling; however, you can expand it to indentation, and etc.
Example ways to minify Python
All white space could be replaced with the minimum possible amount (I acknowledge that in Python you can do some tricky stuff with tabs, but I'll leave that up to you to decide whether or not to implement it).
Example
The following:
def print_a_range(a):
for i in range(a):
print(i)
Could be:
def print_a_range(a):
for i in range(a):
print(i)
Technically, if there is only one line inside a loop, you can compress it even more:
def print_a_range(a):
for i in range(a):print(i) #Note, you can also remove the `()` here.
However, there is another way you can minify white space in Python:
The following:
print ([a * 2 for a in range(20) if a % 2 == 0])
Could be:
print([a*2for a in range(20)if a%2==0])
Note, that there is no need for a space between 2
and for
. Variable, functions, and keywords cannot start with a number. So, the Python interpreter is okay with <num><keyword>
, no space. You should also note there does not have to a space between )
and if
.
Note, you must not change the output of the program! So:
print"f(x)=x*2 is a great equation!"
The above print statement should stay the same because removing the space between 2
and is
would modify the output.
'1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111'
into'1'*100
allowed? Require to do as the behavior is same? \$\endgroup\$