Tsiolkovsky rocket equation
#!/usr/bin/env python3
import math
import sys
wet = float(sys.argv[1])
dry = float(sys.argv[2])
isp = float(sys.argv[3])
g0 = 9.80665
delta_v = isp * g0 * math.log(wet / dry)
print(f"{delta_v:.1f} m/s")
Tsiolkovsky rocket equation
#!/usr/bin/env python3
import math
import sys
wet = float(sys.argv[1])
dry = float(sys.argv[2])
isp = float(sys.argv[3])
g0 = 9.80665
delta_v = isp * g0 * math.log(wet / dry)
print(f"{delta_v:.1f} m/s")
Python replacement for the uniq command.
#!/usr/bin/env python3
import sys
seen = set()
for line in sys.stdin:
if line not in seen:
seen.add(line)
sys.stdout.write(line)
This is not equivalent to sort -u because it preserves the original order, and it’s not equivalent to uniq because it removes duplicates even when they’re not adjacent.
Strip punctuation from stdin
#!/usr/bin/env python3
import sys
for line in sys.stdin:
sys.stdout.write(
.join(c for c in line if c.isalnum() or c.isspace())
)
List bash commands by frequency
#!/usr/bin/env python3
import os
history_file = os.path.expanduser("~/.bash_history")
counts = {}
with open(history_file, "r", errors="ignore") as f:
for line in f:
line = line.strip()
if not line or line.startswith("#"):
continue
cmdline = line.split("|", 1)[0].strip()
parts = cmdline.split()
if not parts:
continue
cmd = parts[1] if parts[0] == "sudo" and len(parts) > 1 else parts[0]
counts[cmd] = counts.get(cmd, 0) + 1
sorted_cmds = sorted(counts.items(), key=lambda x: x[1], reverse=True)
for cmd, n in sorted_cmds:
print(f"{n} {cmd:20}")
Top 20 bash commands in Python
#!/usr/bin/env python3
from collections import Counter
import matplotlib.pyplot as plt
import os
history_file = os.path.expanduser("~/.bash_history")
counter = Counter()
with open(history_file, "r", errors="ignore") as f:
for line in f:
line = line.strip()
if not line or line.startswith("#"):
continue
cmdline = line.split("|", 1)[0].strip()
parts = cmdline.split()
if not parts:
continue
cmd = parts[1] if parts[0] == "sudo" and len(parts) > 1 else parts[0]
counter[cmd] += 1
top = counter.most_common(20)
commands, counts = zip(*top)
commands = commands[::-1]
counts = counts[::-1]
plt.figure(figsize=(10, 6))
plt.barh(commands, counts)
plt.title("Top 20 Bash Commands (frequency)")
plt.xlabel("Usage count")
plt.tight_layout()
plt.show()
Calculate phi with bc and bash
#!/usr/bin/env bash
a=0 b=1
for((i=2;i<=23;i++));{
c=$((a+b))
printf "Fibonacci(%d) / Fibonacci(%d) = %.20f\n" $i $((i-1)) "$(bc -l<<<"$c/$b")"
a=$b b=$c
}
Solve for the intersection of two lines from four points with Python
#!/usr/bin/env python3
import sys,numpy as n
p,q,r,s=map(n.array,((float(sys.argv[i]),float(sys.argv[i+1])) for i in (1,3,5,7)))
M=n.c_[q-p,s-r]
d=n.linalg.det(M)
if abs(d)<1e-10:
print("Coincident" if abs(n.cross(q-p,r-p))<1e-10 else "Parallel")
else:
t,_=n.linalg.solve(M,r-p)
print(p+t*(q-p))
Closing in on a square wave with the Fourier series under Python
#!/usr/bin/env python3
import numpy as n,matplotlib.pyplot as p
x=n.linspace(0,2*n.pi,1000)
def s(x,t):
k=n.arange(1,2*t,2)
return 4/n.pi*(n.sin(k[:,None]*x)/k[:,None]).sum(0)
N=12
colors=p.cm.rainbow(n.linspace(0,1,N))
for i,t in enumerate(range(1,N+1)):
p.plot(x,s(x,t),color=colors[i],label=f'{2*t-1} harmonics')
p.title('Fourier Series Approximation of a Square Wave')
p.grid()
p.legend()
p.show()