Skip to content

Commit

Permalink
Fix typos
Browse files Browse the repository at this point in the history
  • Loading branch information
purplesyringa committed Nov 16, 2024
1 parent f1f83fc commit 8eee88e
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions blog/any-python-program-fits-in-30-characters/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
<span class=hljs-string>" A BC DEFGH I J"</span>
)
)
</code></pre><p>The characters <code>ABCDEFGHIJ</code> are located at indices <eq><math><mrow><mn>9</mn><mo separator=true>,</mo></mrow><mrow><mn>11</mn><mo separator=true>,</mo></mrow><mrow><mn>12</mn><mo separator=true>,</mo></mrow><mrow><mn>28</mn><mo separator=true>,</mo></mrow><mrow><mn>29</mn><mo separator=true>,</mo></mrow><mrow><mn>30</mn><mo separator=true>,</mo></mrow><mrow><mn>31</mn><mo separator=true>,</mo></mrow><mrow><mn>32</mn><mo separator=true>,</mo></mrow><mrow><mn>133</mn><mo separator=true>,</mo></mrow><mrow><mn>160</mn></mrow></math></eq> – all whitespace code points below <eq><math><mn>256</mn></math></eq> except CR and LF, which are invalid in a string. While this code is long, most of it is just whitespace, which we ignore. After removing whitespace, it’s just only <eq><math><mn>32</mn></math></eq> characters:<pre><code class=language-python><span class=hljs-built_in>exec</span>(<span class=hljs-string>""</span>.translate(<span class=hljs-string>"ABCDEFGHIJ"</span>))
</code></pre><p class=next-group><span aria-level=3 class=side-header role=heading><span>Alphabet</span></span>We can now evaluate any Python programs that use at most <eq><math><mn>10</mn></math></eq> different characters.<p>This would be more than enough for JavaScript: <a href=https://jsfuck.com/>JSFuck</a> can transform any JS program to an equivalent JS program that only uses characters from the 6-character set <code>[]()!+</code>. Does anything like this exist for Python?<p>Actually, it does! <a href=https://github.com/kuangkzh/PyFuck>PyFuck</a> transforms any Python script to an equivalent script that uses only 8 characters: <code>exc('%0)</code>. This means that we can reduce our <eq><math><mn>10</mn></math></eq>-byte alphabet to just <eq><math><mn>8</mn></math></eq> bytes, further reducing the code size:<pre><code class=language-python><span class=hljs-built_in>exec</span>(<span class=hljs-string>"[whitespace...]"</span>.translate(<span class=hljs-string>" e xc ('%0)"</span>))
</code></pre><p>The characters <code>ABCDEFGHIJ</code> are located at indices <eq><math><mrow><mn>9</mn><mo separator=true>,</mo></mrow><mrow><mn>11</mn><mo separator=true>,</mo></mrow><mrow><mn>12</mn><mo separator=true>,</mo></mrow><mrow><mn>28</mn><mo separator=true>,</mo></mrow><mrow><mn>29</mn><mo separator=true>,</mo></mrow><mrow><mn>30</mn><mo separator=true>,</mo></mrow><mrow><mn>31</mn><mo separator=true>,</mo></mrow><mrow><mn>32</mn><mo separator=true>,</mo></mrow><mrow><mn>133</mn><mo separator=true>,</mo></mrow><mrow><mn>160</mn></mrow></math></eq> – all whitespace code points below <eq><math><mn>256</mn></math></eq> except CR and LF, which are invalid in a string. While this code is long, most of it is just whitespace, which we ignore. After removing whitespace, it’s only <eq><math><mn>32</mn></math></eq> characters:<pre><code class=language-python><span class=hljs-built_in>exec</span>(<span class=hljs-string>""</span>.translate(<span class=hljs-string>"ABCDEFGHIJ"</span>))
</code></pre><p class=next-group><span aria-level=3 class=side-header role=heading><span>Alphabet</span></span>We can now encode any Python program that uses at most <eq><math><mn>10</mn></math></eq> different characters.<p>This would be more than enough for JavaScript: <a href=https://jsfuck.com/>JSFuck</a> can transform any JS program to an equivalent JS program that only uses characters from the 6-character set <code>[]()!+</code>. Does anything like this exist for Python?<p>Actually, it does! <a href=https://github.com/kuangkzh/PyFuck>PyFuck</a> transforms any Python script to an equivalent script that uses only 8 characters: <code>exc('%0)</code>. This means that we can reduce our <eq><math><mn>10</mn></math></eq>-byte alphabet to just <eq><math><mn>8</mn></math></eq> bytes, further reducing the code size:<pre><code class=language-python><span class=hljs-built_in>exec</span>(<span class=hljs-string>"[whitespace...]"</span>.translate(<span class=hljs-string>" e xc ('%0)"</span>))
</code></pre><p>That’s <eq><math><mn>30</mn></math></eq> characters (plus whitespace).<p class=next-group><span aria-level=3 class=side-header role=heading><span>Optimization</span></span>There’s just one problem: the output of PyFuck is <em>exponential</em> in the count of non-<code>exc(0)</code> characters in the input code. So to encode realistic programs with just <code>exc('%0)</code>, we need to pass code through <em>a nested encoder</em> before passing it to PyFuck. The decoder looks like this:<pre><code class=language-python><span class=hljs-built_in>exec</span>(<span class=hljs-built_in>int</span>(<span class=hljs-string>"[bits of code]"</span>.replace(<span class=hljs-string>"("</span>,<span class=hljs-string>"0"</span>).replace(<span class=hljs-string>")"</span>,<span class=hljs-string>"1"</span>),<span class=hljs-number>2</span>).to_bytes([length of code]))
</code></pre><p>We store bits as <code>(</code> and <code>)</code>, so there’s only a fixed cost due to PyFuck (about <eq><math><mn>400</mn></math></eq> KiB). Plus bits of the code, taking <eq><math><mrow><mn>8</mn><mo>×</mo></mrow></math></eq> more than the original bytes, but that’s nothing compared to the PyFuck overhead.<p class=next-group><span aria-level=3 class=side-header role=heading><span>The end</span></span>So that’s how you print <em>Lorem Ipsum</em> in only <eq><math><mn>30</mn></math></eq> characters and just <eq><math><mn>420</mn></math></eq> KiB of whitespace (still smaller than Electron). <a href=https://github.com/purplesyringa/30-characters-of-python>Check out the repo on GitHub.</a><p>Hope you found this entertaining! If anyone knows how to bring this to <eq><math><mn>29</mn></math></eq> characters or less, I’m all ears. :)</div></section><footer><div class=viewport-container><h2>Made with my own bare hands (why.)</h2></div></footer><script>window.addEventListener("keydown", e => {
if (e.key === "Enter") {
Expand Down
4 changes: 2 additions & 2 deletions blog/any-python-program-fits-in-30-characters/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ exec(
)
```

The characters `ABCDEFGHIJ` are located at indices $9, 11, 12, 28, 29, 30, 31, 32, 133, 160$ -- all whitespace code points below $256$ except CR and LF, which are invalid in a string. While this code is long, most of it is just whitespace, which we ignore. After removing whitespace, it's just only $32$ characters:
The characters `ABCDEFGHIJ` are located at indices $9, 11, 12, 28, 29, 30, 31, 32, 133, 160$ -- all whitespace code points below $256$ except CR and LF, which are invalid in a string. While this code is long, most of it is just whitespace, which we ignore. After removing whitespace, it's only $32$ characters:

```python
exec("".translate("ABCDEFGHIJ"))
Expand All @@ -72,7 +72,7 @@ exec("".translate("ABCDEFGHIJ"))

### Alphabet

We can now evaluate any Python programs that use at most $10$ different characters.
We can now encode any Python program that uses at most $10$ different characters.

This would be more than enough for JavaScript: [JSFuck](https://jsfuck.com/) can transform any JS program to an equivalent JS program that only uses characters from the 6-character set `[]()!+`. Does anything like this exist for Python?

Expand Down

0 comments on commit 8eee88e

Please sign in to comment.