Python on your phone sucks less with `ooo.py` and the `-i` flag.

TLDR: Copy the script below, and run it as python -i ooo.py. This will give you a bunch of useful imports with short aliases, and throw you into an interactive shell.

'''Run as `python3 -i ooo.py`

I use this when writing Python scripts on my phone (through Termux). The point is to minimize the keypresses needed when writing Python on a phone.

This will start Python, import a bunch of modules with short names, and then throw you into an interpreter.
'''

import numpy as np
import pandas as pd
import math as mt
import random as rd
import time
import itertools as it
import functools as ft
import re
import os
import sys
import glob
import pickle as pl
import hashlib as hl
import secrets as ss
import socket as sk

print("imported numpy as np, pandas as pd, math as mt, random as rd, time, itertools as it, functools as ft, re, os, sys, glob, pickle as pl, hashlib as hl, secrets as ss, and socket as sk!")

A = np.array

PI = mt.pi
TAU = mt.tau
E = mt.e

print("Constants available: PI, TAU, E .")

H = help
Q = quit

print("Functions `help(...)` and `quit()` available as `H(...)` and `Q()`.")
Read more  ↩︎

You can sort in linear time!

A log-scale graph showing the runtime (ns) of sorting N elements for different integer depths. They all show a trend towards linear runtime for large N.

A log-scale graph showing the linear runtime of count-sort

TLDR: Yes-- you can sort numbers in linear time. Yes, including floats! The proof that you can sort in $O(n \log n)$ time is still true, but that assumes you're sorting on the infinite set of real numbers (or integers.) We can sort in linear time because we work with finite sets: Floats and ints. The algorithm is very simple, and is called counting sort (or bin sort or radix sort.)

See the Python notebook wrapping an unoptimized Rust countsort implementation:

--

Is this truly $O(n)$? Yes!

  • Counting sort is not a randomized algorithm, it always outputs the correct solution.
  • Counting sort is not a parallelized algorithm. (And you can't use parallelism to speed past a worst-case big-O run time anyway!)
  • Counting sort is not an amortized algorithm. It is truly $O(n)$, not 'expected' or 'amortized' $O(n)$.
  • Counting sort is technically $Θ(n)$, that is, the worst and best case running time is linear.
  • This can be used in the real world.
  • However, it is not an in-place sorting algorithm.
Read more  ↩︎

`reso`, a colorful pixel-art circuit simulator

Reso is a circuit design language and simulator where the inputs and outputs are bitmap images!

Reso logo, incrementing over a truth table in an artistically-crafted circuit. Outputting AND, OR, and XOR to the diamonds.

Reso is a pet project of mine that I've been working on in my free time for a little while. This logo is actually an animation of the execution of a Reso circuit.

Read more  ↩︎

Oh no, dating spam sites are abusing improperly-configured internal search engines

tldr: Malicious entities are abusing sites internal search engines to promote their own URLs. This effectively allows them to advertise using the target site's domain in the search results of major search engines. If you own such a site, fix this by putting a disallow entry in your robots.txt, or adding the noindex meta tag.

Read more  ↩︎

GIMP <3 Python! Let's make a YuGiOh rip off

tldr: Open the Python-Fu console (Filters > Python-Fu > Console) and reference GIMP's built-in pdb documentation (Help > Procedure browser). A simple 'hello world' is presented:

img = gimp.image_list()[0]
title_layer = pdb.gimp_image_get_layer_by_name(img, 'Title')
pdb.gimp_text_layer_set_text(title_layer, "Hello world!")
Read more  ↩︎