Category Archives: Uncategorized

Deltav

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”)  

Posted in Uncategorized | Leave a comment

Unik

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 … Continue reading

Posted in Uncategorized | Leave a comment

Nopunc

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()) )

Posted in Uncategorized | Leave a comment

Cmdlog

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 = … Continue reading

Posted in Uncategorized | Leave a comment

Commands

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 … Continue reading

Posted in Uncategorized | Leave a comment

Phi

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 }

Posted in Uncategorized | Leave a comment

Intersect

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))

Posted in Uncategorized | Leave a comment

Square

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 … Continue reading

Posted in Uncategorized | Leave a comment