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

4let

Print all the four letter words in the Bible:

perl -ne 'for(/\w+/g){$w=lc; $seen{$w}=1 if length$w==4} END{print"$_ " for sort keys %seen}' kjv.txt

Posted in Uncategorized | Leave a comment

Factorials

Calculate factorials over a range

#!/usr/bin/env python3
import sys
import functools
start = int(sys.argv[1])
end = int(sys.argv[2])
for n in range(start, end + 1):
    factorial = 1 if n == 0 else functools.reduce(
        lambda x, y: x * y, range(1, n + 1)
    )
    print(f"{n}! = {factorial}")

Posted in Uncategorized | Leave a comment

3D Cube

#!/usr/bin/python3
import matplotlib.pyplot as plt
import numpy as np
axes = [5, 5, 5]
data = np.ones(axes, dtype=bool)
alpha = 0.9
colors = np.empty(axes + [4], dtype=np.float32)
colors[0] = [1, 0, 0, alpha]  # red
colors[1] = [0, 1, 0, alpha]  # green
colors[2] = [0, 0, 1, alpha]  # blue
colors[3] = [1, 1, 0, alpha]  # yellow
colors[4] = [1, 1, 1, alpha]  # white
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.voxels(data, facecolors=colors, edgecolors='grey')
plt.show()

Posted in Uncategorized | Leave a comment