Build a calendar in Python
import calendar
calendar.setfirstweekday(6)
calendar.prcal(2026)
Additive synthesis of square wave and sawtooth wave
#!/usr/bin/python3
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, 2 * np.pi, 0.01)
y = np.zeros_like(x)
for n in range(1, 16, 2):
y += np.sin(n * x) / n
z = np.zeros_like(x)
for n in range(1, 9):
z += np.sin(n * x) / n
plt.figure(figsize=(10, 5))
plt.plot(x, y, label="Odd harmonics (1, 3, 5, ..., 15)")
plt.plot(x, z, color="red", label="All harmonics (1 through 8)")
plt.xlim(0, 2 * np.pi)
plt.xticks(
[0, np.pi/2, np.pi, 3*np.pi/2, 2*np.pi],
["0", "π/2", "π", "3π/2", "2π"]
)
plt.xlabel("Radians")
plt.ylabel("Amplitude")
plt.title("Fourier Series")
plt.grid(True)
plt.legend()
plt.tight_layout()
plt.show()
Random name generator
#!/usr/bin/python3
import sys
import random
from pathlib import Path
script_dir = Path(__file__).resolve().parent
with open(script_dir / "fnames.idx", "r") as female:
lines1 = female.readlines()
with open(script_dir / "mnames.idx", "r") as male:
lines2 = male.readlines()
with open(script_dir / "lnames.idx", "r") as last:
lines3 = last.readlines()
random.shuffle(lines1)
random.shuffle(lines2)
random.shuffle(lines3)
sys.stdout.write("Male\n\n")
for i in range (0, 10, 1):
sys.stdout.write(lines2[i].rstrip())
sys.stdout.write(" ")
sys.stdout.write(lines3[i])
sys.stdout.write("\nFemale\n\n")
for i in range (11, 21, 1):
sys.stdout.write(lines1[i].rstrip())
sys.stdout.write(" ")
sys.stdout.write(lines3[i])
Using Python and linear algebra to find the intersection of two lines, each line defined by four points.
#!/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))
A compact recursive-backtracking maze generator in #Python using a randomized depth-first search to carve passages through a rectangular grid.
#!/usr/bin/python3
from random import shuffle, randrange
def makemaze(w=20,h=12):
vis = [[0] * w + [1] for _ in range(h)] + [[1] * (w + 1)]
ver = [["| "] * w + ['|'] for _ in range(h)] + [[]]
hor = [["+--"] * w + ['+'] for _ in range(h + 1)]
def walk(x, y):
vis[y][x] = 1
d = [(x - 1, y), (x, y + 1), (x + 1, y), (x, y - 1)]
shuffle(d)
for (xx, yy) in d:
if vis[yy][xx]: continue
if xx == x: hor[max(y, yy)][x] = "+ "
if yy == y: ver[y][max(x, xx)] = " "
walk(xx, yy)
walk(randrange(w), randrange(h))
s = ""
for (a, b) in zip(hor, ver):
s += ''.join(a + ['\n'] + b + ['\n'])
return s
print(makemaze())
Golden-section search to find the positive minimum of the Gamma function.
#!/usr/bin/env python3
from scipy.special import gamma
a,b=.1,2
n=25
p=(5**.5-1)/2
c=b-p*(b-a)
d=a+p*(b-a)
fc,fd=gamma(c),gamma(d)
print(f'{"Iter":>4} {"x":>12} {"gamma(x)":>12}')
for i in range(1,n+1):
if fc<fd:
b,d,fd=d,c,fc
c=b-p*(b-a)
fc=gamma(c)
x,f=c,fc
else:
a,c,fc=c,d,fd
d=a+p*(b-a)
fd=gamma(d)
x,f=d,fd
print(f"{i:4d} {x:12.8f} {f:12.8f}")
print(f"\nLocal minimum approx: x = {x:.10f}, gamma(x) = {f:.10f}")
from datetime import date, timedelta
def thanksgiving(year):
nov1 = date(year, 11, 1)
first_thursday = nov1 + timedelta(days=(3 - nov1.weekday()) % 7)
return first_thursday + timedelta(weeks=3)
List all binaries in /usr/bin which do 𝑛𝑜𝑡 have a man page #linux
find /usr/bin -exec basename {} \; | xargs whereis -m | grep -v \/