Contour

Contour map with Python

#!/usr/bin/env python3
import matplotlib.pyplot as p,numpy as n
x,y=n.meshgrid(n.linspace(-4,4,512),n.linspace(-4,4,512))
z=(1-x/2+x**4+y**3)*n.exp(-x**2-y**2)*(1-x/3-y**4)*(3-y+x**2)
z+=.5*n.sin(.8*x+.6*y)+.4*n.cos(.5*x-.7*y)+.3*n.sin(.6*x-.4*y)
p.contour(x,y,z,levels=n.linspace(z.min(),z.max(),25),cmap='terrain')
p.show()

Posted in Uncategorized | Leave a comment

Names

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

Posted in Uncategorized | Leave a comment

Intersect

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

Posted in Uncategorized | Leave a comment

Maze

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

Posted in Uncategorized | Leave a comment

Gamma

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

Posted in Uncategorized | Leave a comment

Thanksgiving

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)

Posted in Uncategorized | Leave a comment

Toot TUI

Toot TUI, Mastodon at the terminal:

Posted in Uncategorized | Leave a comment

Streamripper

Posted in Uncategorized | Leave a comment

Whereis

List all binaries in /usr/bin which do 𝑛𝑜𝑡 have a man page #linux

find /usr/bin -exec basename {} \; | xargs whereis -m | grep -v \/

Posted in Uncategorized | Leave a comment

Easter

Calculate the date of Easter, accurate from 1900 to 2368 inclusive, with Python

Easter is celebrated on the first Sunday after the first full moon on or after 21 March.

#!/usr/bin/python3
import sys
y=int(sys.argv[1])
c=y//100;n=y-19*(y//19);k=(c-17)//25;i=c-c//4-(c-k)//3+19*n+15
i=i-30*(i//30);i=i-(i//28)*(1-(i//28)*(29//(i+1))*((21-n)//11))
j=y+y//4+i+2-c+c//4;j=j-7*(j//7);l=i-j;m=3+(l+40)//44;d=l+28-31*(m//4)
print(f"{y:04d}-{m:02d}-{d:02d}")

Posted in Uncategorized | Leave a comment