Python code to generate 26 page headers for a chapter of my magnum opus:
Posts
Python code to generate 26 page headers for a chapter of my magnum opus:
Python script to detect the width of your text and put a box around it
#!/usr/bin/env python3
import sys
lines = [line.rstrip("\n") for line in sys.stdin]
if not lines:
sys.exit()
width = len(lines[0])
for n, line in enumerate(lines, 1):
if len(line) > width:
sys.exit(f"Line {n} is {len(line)} characters, exceeds box width {width}")
print("+" + "-" * (width + 2) + "+")
for line in lines:
print(f"| {line:<{width}} |")
print("+" + "-" * (width + 2) + "+")
Skyglobe for MSDOS, still the best astronomy software 35 years later…
Before we wasted time on Windows solitaire we wasted time on MSDOS solitaire.
while :;do echo $LINES $COLUMNS $((RANDOM%COLUMNS)) $(printf '%b' "\\u$(printf '%04x' $((RANDOM%500)))");sleep .05;done|gawk '{a[$3]=0;for(x in a){o=a[x];a[x]++;printf "\033[%s;%sH\033[2;32m%s",o,x,$4;printf "\033[%s;%sH\033[1;37m%s\033[0;0H",a[x],x,$4;if(a[x]>=$1)a[x]=0}}'
Generate magic squares for any odd side length:
#!/usr/bin/env python3
import sys
def magic_constant(n):
return n * (n * n + 1) // 2
def magic_square(n):
square = [[0] * n for _ in range(n)]
num = 1
i, j = 0, n // 2
while num <= n * n:
square[i][j] = num
num += 1
new_i = (i - 1) % n
new_j = (j + 1) % n
if square[new_i][new_j]:
i = (i + 1) % n
else:
i, j = new_i, new_j
return square
n = int(sys.argv[1]) if len(sys.argv) > 1 else 3
square = magic_square(n)
width = len(str(n * n))
print(f"Magic constant for n={n}: {magic_constant(n)}\n")
for row in square:
print(" ".join(f"{x:>{width}}" for x in row))
Print a list of last Fridays every month for the given year
#!/usr/bin/python3
import sys
from calendar import monthrange
from datetime import date, timedelta
def last_fridays(year):
for month in range(1, 13):
last_day = date(year, month, monthrange(year, month)[1])
yield last_day - timedelta(days=(last_day.weekday() - 4) % 7)
for friday in last_fridays(int(sys.argv[1])):
print(friday.isoformat())
Print a list of antiprimes (highly composite numbers) with bc
#! /usr/bin/bc
define factors(n) {
auto c, i
for(i=1;i<=n;i++) {
if(n%i==0) c+=1
}
return c
}
max = 0
n = 1
for(c=19; c > 0;) {
f = factors(n)
if(f > max) {
print n," "
max = f
c -= 1
}
n += 1
}
print "\n"
quit