All the vowels

With Python find words longer than 10 characters that use all the vowels:

#! /usr/bin/env python3
with open('unixdict.txt') as f:
    while (line := f.readline().strip()):
        if (len(line) > 10 and all(
                line.count(c) == 1 for c in 'aeiou')):
            print(line)

Posted in Uncategorized | Leave a comment

Say

Number to words

#!/usr/bin/env python3
import sys
n = int(sys.argv[1])
ones = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
teens = ["ten", "eleven", "twelve", "thirteen", "fourteen","fifteen", "sixteen", "seventeen", "eighteen", "nineteen"]
tens = ["", "", "twenty", "thirty", "forty","fifty", "sixty", "seventy", "eighty", "ninety"]
if n < 10:
    print(ones[n])
elif n < 20:
    print(teens[n - 10])
else:
    t = n // 10
    o = n % 10
    if o == 0:
        print(tens[t])
    else:
        print(f"{tens[t]}-{ones[o]}")
Posted in Uncategorized | Leave a comment

Spiral

Archimedes spiral with turtle graphics

#!/usr/bin/env python3
from turtle import *
from math import *
color("blue")
speed(0)
down()
for i in range(200):
    t = i*pi/20
    r = 1+5*t
    goto(r*cos(t), r*sin(t))
done()

Posted in Uncategorized | Leave a comment

Compile

Compile short C programs on the fly at the command prompt

echo 'main() { printf("Hello world!\n"); }' | gcc -w -x c - -o hello
echo '#include <stdio.h>
int fibonacci(int n){return n<2?n:fibonacci(n-1)+fibonacci(n-2);}
int main(){printf("%d\n",fibonacci(19));return 0;}' | gcc -x c - -o fib19

Posted in Uncategorized | Leave a comment

Nth prime

Calculate the nth prime with Python

#!/usr/bin/env python3
import sys
import time; max=int(sys.argv[1]); n=1; p=1
t0 = time.perf_counter()
while n<=max:
    f=0; j=2; s = int(p**0.5)
    while f < 1:
        if j >= s:
            f=2
        if p % j == 0:
            f=1
        j+=1
    if f != 1:
        n+=1;
        #print(n,p);
    p+=1
t1 = time.perf_counter()
print(f"n = {max}")
print(f"nth prime = {p-1}")
print(f"Elapsed time = {t1-t0:.6f} seconds")

Posted in Uncategorized | Leave a comment

Payload

Iteratively converge on LEO-deliverable payload mass for the Astrodyne Argo Epsilon single-booster first stage (RP-1/LOX) and Delta second stage (LH2/LOX) stack:

#!/usr/bin/env python3
import math
S1_WET = 46357.2
S1_DRY = 3708.6
S1_ISP = 280.0
S2_WET = 19322.5
S2_DRY = 2318.7
S2_ISP = 430.0
TARGET_DV = 7800.0
G0 = 9.80665
def stage_dv(wet, dry, payload, isp):
    m0 = wet + payload
    m1 = dry + payload
    return isp * G0 * math.log(m0 / m1)
def total_dv(payload):
    dv2 = stage_dv(
        S2_WET,
        S2_DRY,
        payload,
        S2_ISP,
    )
    stage2_stack = S2_WET + payload

    dv1 = stage_dv(
        S1_WET,
        S1_DRY,
        stage2_stack,
        S1_ISP,
    )
    return dv1, dv2, dv1 + dv2
low = 0.0
high = 100000.0
for _ in range(60):
    payload = (low + high) / 2
    dv1, dv2, total = total_dv(payload)
    if total > TARGET_DV:
        low = payload
    else:
        high = payload
payload = (low + high) / 2
dv1, dv2, total = total_dv(payload)
print(f"Target delta-v : {TARGET_DV:10.1f} m/s")
print(f"Payload        : {payload:10.1f} kg")
print(f"Stage 1 Δv     : {dv1:10.1f} m/s")
print(f"Stage 2 Δv     : {dv2:10.1f} m/s")
print(f"Total Δv       : {total:10.1f} m/s")

Posted in Uncategorized | Leave a comment

Ellipse

#! /usr/bin/python3
import math
import sys
a = float(sys.argv[1])
b = float(sys.argv[2])
h = ((a - b) / (a + b))**2
eps = sys.float_info.epsilon
def binom_half(n):
    coef = 1.0
    for k in range(n):
        coef *= (0.5 - k) / (k + 1)
    return coef
perimeter = 0.0
n = 0
term = 1.0  
while abs(term) > eps:
    coef = binom_half(n)
    term = (coef**2) * h**(2*n)
    perimeter += term
    n += 1
perimeter *= math.pi * (a + b)
print(f"Ellipse perimeter: {perimeter:.17f}")
print(f"Series converged after {n} terms")

Posted in Uncategorized | Leave a comment

Ellipsoid

#! /usr/bin/env python3
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
r = 10
u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, np.pi, 100)
x = r * np.outer(np.cos(u), np.sin(v))
y = r * np.outer(np.sin(u), np.sin(v))
z = r * np.outer(np.ones(np.size(u)), np.cos(v))
ax.plot_surface(x, y, z, color='linen', alpha=0.5)
theta = np.linspace(0, 2 * np.pi, 100)
z = np.zeros(100)
x = r * np.sin(theta)
y = r * np.cos(theta)
ax.plot(x, y, z, color='black', alpha=0.75)
ax.plot(z, x, y, color='black', alpha=0.75)
zeros = np.zeros(1000)
line = np.linspace(-10,10,1000)
ax.plot(line, zeros, zeros, color='black', alpha=0.75)
ax.plot(zeros, line, zeros, color='black', alpha=0.75)
ax.plot(zeros, zeros, line, color='black', alpha=0.75)
plt.show()

Posted in Uncategorized | Leave a comment

Say

#!/usr/bin/env python3
import sys
n = int(sys.argv[1])
ones = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
teens = ["ten", "eleven", "twelve", "thirteen", "fourteen","fifteen", "sixteen", "seventeen", "eighteen", "nineteen"]
tens = ["", "", "twenty", "thirty", "forty","fifty", "sixty", "seventy", "eighty", "ninety"]
if n < 10:
    print(ones[n])
elif n < 20:
    print(teens[n - 10])
else:
    t = n // 10
    o = n % 10
    if o == 0:
        print(tens[t])
    else:
        print(f"{tens[t]}-{ones[o]}")

Posted in Uncategorized | Leave a comment

Truthtable

Boolean truth tables in Python

#!/usr/bin/env python3
import sys
from itertools import product
bexp=" ".join(sys.argv[1:])
code=compile(bexp,"<string>","eval")
names=code.co_names
print("\n" + " ".join(names),":",bexp)
for values in product(range(2), repeat=len(names)):
    env=dict(zip(names,values))
    print(" ".join(map(str,values)),":",int(eval(code,env)))

Posted in Uncategorized | Leave a comment