Python code golfer and Python code minifier Inspired by dflook's minifier
- Demo
- Examples
2.1 Primes
2.2 Greatest Common Divisor
2.3 Biggest int - Q&A
3.1. Why ?
3.2. How does it work ?
3.3. Why is sometime a white space added to the end ?
3.4. Does it always make my code shorter ?
3.5. Are there other automated methods to shorten Python code ?
3.6. Can I contribute ? - Known bugs
4.1. Reversed coated code
- Go to the web page
- Wait until the result box displays "Ready!":
- Enter your Python code in the top box:
- Click the "Golf it!" button
- Get the result from the bottom box
- Try it online!
Problem statement: In a range from 1 to N
included, print the number followed by True if this number is prime, False otherwise. (Example: 7: True
, or 8: False
)
Input code:
n = int(input())
for i in range(1,n+1):
if all(i%d!=0 for d in range(2,i)):
print(str(i)+": True")
else:
print(str(i)+": False")
Result:
exec(bytes('‽湩⡴湩異⡴⤩昊牯椠椠慲杮⡥ⰱ⭮⤱††晩愠汬椨搥㴡‰潦湩爠湡敧㈨椬⤩††††牰湩⡴瑳⡲⥩∫›牔敵⤢ †攠獬㩥 †††瀠楲瑮猨牴椨⬩㨢䘠污敳⤢','u16')[2:])
Stats: 101 chars - 51 diff - 66.45% of the original size (-33.55%)
Problem statement: Print the greatest common divisor between the two givens numbers.
Input code:
a, b = map(int, input().split())
def gcd(a, b):
while b:
a, b = b, a%b
return a
print(gcd(a,b))
Result:
exec(bytes('ⱡ戠㴠洠灡椨瑮湩異⡴⸩灳楬⡴⤩搊晥朠摣愨⥢††桷汩㩢 †††愠‽Ɫ愠戥 †爠瑥牵牰湩⡴捧⡤ⱡ⥢
','u16')[2:])
Stats: 81 chars - 31 diff - 72.32% of the original size (-27.68%)
Problem statement: Print the maximum int in a list.
Input code:
n = map(int, input().split())
m = 0
for e in n:
if e > m:
m = e
print(m)
Result:
exec(bytes('‽慭⡰湩ⱴ椠灮瑵⤨献汰瑩⤨‽ਰ潦湩渠††晩攠㸠洠††††‽牰湩⡴⥭','u16')[2:])
Stats: 67 chars - 17 diff - 79.76% of the original size (-20.24%)
In some cases, like in Codingame's Clash of Code or code.golf for example, the goal is to make a program to solve a problem but the program needs to be the shortest possible (with the less chars if you prefer). Python is pretty efficient for this, but you can win even more chars by converting the characters of your code from utf-8 to utf-16. This is very simple to do with IDLE for instance, but this sometime takes too much time and is repetitive. This project aims to make this process faster.
Your input is converted by the golfcode
function in the script.js file. In fact, every pair of two adjacent chars in your input code is jammed together to make only 1 char, resulting in a golfed code length of x/2+25, given that x is the length of your input code.
A single utf-16 is represented by two utf-8 chars. It means that is the length of your input code is not even, it is impossible to golf it using this method. To alleviate this problem, it simply adds a white space at the end of your code.
No, it basically can not do this. To execute the given code, it must be coated with some additional code. This always add length to the inputed code. If your code is very long, it will obviously make it shorter, but if not, it might not. There are some statistics at the bottom of the page, you can see there if you won some chars.
Yes, they are. For instance there is dflook's Python minifier which is working fine for some inputs. Those two tools combined can be very powerful to golf code fast.
Of course! Fork the repo and create a pull request.
Where the ungolfed code contains the precise string .
or +
, for some reasons, the coated string after the returned code is reversed. It turns into ([:2]('61u','
. Also, this string appears in the middle. (See #2)