Post

Python unicode compress

Python unicode compress

Simple tools for golf

쓸데 없지만 재미있는 기?술. https://code.golf 용.

2:1 pack

Compress

Your code length must be even. Don’t use """.

Decompress

Examples

https://code.golf/tutorial#python: Codegolf tutorial problem

다음 두 코드는 동일한 출력을 생성하나, 아래 코드의 char 수가 67(위)에서 59로 단축됨.

import sys;print('Hello, World!',*range(10),*sys.argv[1:],sep='\n')

exec(bytes('浩潰瑲猠獹瀻楲瑮✨效汬Ɐ圠牯摬✡⨬慲杮⡥〱Ⱙ猪獹愮杲孶㨱ⱝ敳㵰尧❮
','u16')[2:])

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
>>> def calc(text):
...     char_count = len(text)
...     byte_count = len(text.encode('utf-8'))
...     print(f"byte: {byte_count}, char: {char_count}")
... 

>>> ANS = r"""import sys;print('Hello, World!',*range(10),*sys.argv[1:],sep='\n')"""
>>> calc(ANS)
byte: 67, char: 67

>>> # to make even
>>> ANS = r"""import sys;print('Hello, World!',*range(10),*sys.argv[1:],sep='\n') """ 
>>> print(ANS.encode().decode('u16'))
浩潰瑲猠獹瀻楲瑮效汬Ɐ圠牯摬✡⨬慲杮〱Ⱙ猪獹愮杲孶㨱ⱝ敳㵰尧❮

>>> compressed_ANS = """exec(bytes('浩潰瑲猠獹瀻楲瑮✨效汬Ɐ圠牯摬✡⨬慲杮⡥〱Ⱙ猪獹愮杲孶㨱ⱝ敳㵰尧❮
','u16')[2:])"""

>>> calc(compressed_ANS)
byte: 127, char: 59

>>> # executed normally
>>> exec(bytes('浩潰瑲猠獹瀻楲瑮✨效汬Ɐ圠牯摬✡⨬慲杮⡥〱Ⱙ猪獹愮杲孶㨱ⱝ敳㵰尧❮
','u16')[2:])
Hello, World!
0
1
2
3
4
5
6
7
8
9
>>> 
This post is licensed under CC BY 4.0 by the author.

Trending Tags