Quadratic

#!/usr/bin/env python3
import math,sys
a,b,c=map(float,sys.argv[1:])
d=b*b-4*a*c
if d>=0:x1=(-b+math.sqrt(d))/(2*a);x2=(-b-math.sqrt(d))/(2*a)
else:r=-b/(2*a);i=math.sqrt(-d)/(2*a);x1,x2=complex(r,i),complex(r,-i)
print("The function has","two real roots: {} and {}".format(x1,x2)if d>0 else
"one double root: {}".format(x1)if d==0 else
"two complex roots: {} and {}".format(x1,x2))
Posted in Uncategorized | Leave a comment

Trig

Trig functions

#! /usr/bin/bc -lq
define t(x) {
        return s(x) / c(x)
}
define y(y) {
        if (y == -1) return -2 * a(1)  /* -pi/2 */
        if (y == 1) return 2 * a(1)    /* pi/2 */
        return a(y / sqrt(1 - y * y))
}
define x(x) {
        auto angle
        if (x == 0) return 2 * a(1)    /* pi/2 */
        angle = a(sqrt(1 - x * x) / x)
        if (x < 0) return angle + 4 * a(1)
        return angle
}
p = 4 * a(1)  /* pi */
d = p / 180   /* one degree in radians */

print "Using radians:\n"
"sin(-pi / 6) = "; s(-p / 6)
"cos(3 * pi / 4) = "; c(3 * p / 4)
"tan(pi / 3) = "; t(p / 3)
"asin(-1 / 2) = "; y(-1 / 2)
"acos(-sqrt(2) / 2) = "; x(-sqrt(2) / 2)
"atan(sqrt(3)) = "; a(sqrt(3))
print "\n\nUsing degrees:\n"
"sin(-30) = "; s(-30 * d)
"cos(135) = "; c(135 * d)
"tan(60) = "; t(60 * d)
"asin(-1 / 2) = "; y(-1 / 2) / d
"acos(-sqrt(2) / 2) = "; x(-sqrt(2) / 2) / d
"atan(sqrt(3)) = "; a(sqrt(3)) / d
quit

Posted in Uncategorized | Leave a comment

Pay

#!/usr/bin/env python3
import sys
from num2words import num2words
def spell_number(my_number):
    my_number = my_number.strip()
    if not my_number:
        return ""
    result = num2words(
        float(my_number),
        to="currency",
        currency="USD"
    )
    result = result.replace(",", "")
    result = result.replace(" and ", " ")
    result = result.replace("-", " ")
    result = result.title()
    result = result.replace(" Dollars ", " Dollars And ")
    result = result.replace(" Dollar ", " Dollar And ")
    return result
def main():
    for line in sys.stdin:
        result = spell_number(line)
        if result:
            print(result)

if __name__ == "__main__":
    main()

Posted in Uncategorized | Leave a comment

Ac

Report statistics of uptime

ac -dp –print-year

 

Posted in Uncategorized | Leave a comment

Abundant

#!/usr/bin/python3
import sys; beg,end=int(sys.argv[1]),int(sys.argv[2]);[print(f"{n} is "+("perfect" if (s:=sum(x for x in range(1,1+n//2) if n%x==0))==n else "abundant" if s>n else "deficient")) for n in range(beg,end+1)]

Posted in Uncategorized | Leave a comment

abcde

abcde for Linux in the off-hand chance you still need to rip a CD.

 

Posted in Uncategorized | Leave a comment

Palin

Find all palindromic words in a file

#!/usr/bin/python3
def words(fileobj):
    for line in fileobj:
        for word in line.split():
            yield word
with open("words.txt") as wordfile:
    wordgen = words(wordfile)
    for word in wordgen:
        if word == word[::-1]:
            print(word, end=" ")
    print()

Posted in Uncategorized | Leave a comment

Paginate

Python code to generate 26 page headers for a chapter of my magnum opus:

 #!/usr/bin/env python3
 import sys
 import string
 letter = sys.argv[1].upper()
 for second in string.ascii_uppercase:
     print(f'<span style="font-size: 1.7em; font-weight: bold;">{letter}{second}</span>')

Posted in Uncategorized | 1 Comment

Boxn

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

Posted in Uncategorized | Leave a comment

MSDOS



Skyglobe for MSDOS, still the best astronomy software 35 years later…



Before we wasted time on Windows solitaire we wasted time on MSDOS solitaire.







Posted in Uncategorized | Leave a comment