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")
Use Python to reverse stdin by words (rev will do this by characters)
#!/usr/bin/env python3
import sys
for line in sys.stdin:
print(' '.join(line.split()[::-1]))
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
}
Generating the Weierstrass Monster with #Python
This is pathology of calculus that is continuous everywhere but differentiable nowhere. Henri Poincaré condemned it as “an outrage against common sense.” Charles Hermite called it a “deplorable evil.” Muahahahaha!
#!/usr/bin/env python3
import numpy as n,matplotlib.pyplot as p
a=.5;b=11;n_terms=50
x=n.linspace(-2,2,20000)
f=sum(a**k*n.cos(b**k*n.pi*x) for k in range(n_terms))
p.plot(x,f,'b',lw=.1)
p.title('Weierstrass Function')
p.grid()
p.show()
Calculate parallel resistance (or series capacitance) with Python
#!/usr/bin/env python3 import sys,numpy as n print(sys.argv[1:],'->',1/(1/n.array(sys.argv[1:],float)).sum())
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))