Deal

Shuffle and deal four hands of five cards each with #Python

#!/usr/bin/python3
import random
deck = [
'๐Ÿ‚ก','๐Ÿ‚ข','๐Ÿ‚ฃ','๐Ÿ‚ค','๐Ÿ‚ฅ','๐Ÿ‚ฆ','๐Ÿ‚ง','๐Ÿ‚จ','๐Ÿ‚ฉ','๐Ÿ‚ช','๐Ÿ‚ซ','๐Ÿ‚ญ','๐Ÿ‚ฎ',
'๐Ÿ‚ฑ','๐Ÿ‚ฒ','๐Ÿ‚ณ','๐Ÿ‚ด','๐Ÿ‚ต','๐Ÿ‚ถ','๐Ÿ‚ท','๐Ÿ‚ธ','๐Ÿ‚น','๐Ÿ‚บ','๐Ÿ‚ป','๐Ÿ‚ฝ','๐Ÿ‚พ',
'๐Ÿƒ','๐Ÿƒ‚','๐Ÿƒƒ','๐Ÿƒ„','๐Ÿƒ…','๐Ÿƒ†','๐Ÿƒ‡','๐Ÿƒˆ','๐Ÿƒ‰','๐ŸƒŠ','๐Ÿƒ‹','๐Ÿƒ','๐ŸƒŽ',
'๐Ÿƒ‘','๐Ÿƒ’','๐Ÿƒ“','๐Ÿƒ”','๐Ÿƒ•','๐Ÿƒ–','๐Ÿƒ—','๐Ÿƒ˜','๐Ÿƒ™','๐Ÿƒš','๐Ÿƒ›','๐Ÿƒ','๐Ÿƒž'
]
random.shuffle(deck)
for i in range(0, 20, 5):
    print(" ".join(deck[i:i+5]))

Posted in Uncategorized | Leave a comment

Perl dupes

Highlight consecutive duplicated words with Perl

perl -pe 's/\b(\w+)\s+\1\b/\e[31m$&\e[0m/gi' gettysburg.txt

Posted in Uncategorized | Leave a comment

Factorial Primes

Factorial primes with gawk

#! /usr/bin/gawk -f
function isprime(n, i) {
  if(n==2)return 1
  if(n<2||n%2==0)return 0
  for(i=3;i*i<=n;i+=2)
    if(n%i==0)return 0
  return 1
}
BEGIN {
  for(x=i=1;z<10;i++){
    x*=i
    if(isprime(x-1)){z++;print i"! - 1 = "x-1}
    if(isprime(x+1)){z++;print i"! + 1 = "x+1}
  }
}

The next output would be (if you had the patience to do it):

27! + 1 = 10,888,869,450,418,352,160,768,000,001

Posted in Uncategorized | Leave a comment

Trig

Trig functions

#! /usr/bin/bc -l
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 a
         if (x == 0) return 2 * a(1)  /* pi/2 */
        a = a(sqrt(1 - x * x) / x)
        if (a < 0) {
                return a + 4 * a(1)  /* add pi */
        } else {
                return a
        }
}
p = 4 * a(1)  /* pi */
d = p / 180   /* one degree in radians */
"Using radians:
"
"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))

"Using degrees:
"
"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

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

1 Liners

Print all primes less than 5000:

$ python3 -c 'print([i for i in range(2,5000) if all(i%j for j in range(2,int(i**0.5)+1))])'

Break a GIF into individual frames:

ffmpeg -i taarna.gif frame_%d.jpg

List all the words in a file with no matches in the dictionary:

aspell list < huckfinn.txt

Play a G major chord on ‘guitar’ using the sox package:

play -n synth pl G2 pl B2 pl D3 pl G3 pl D4 pl G4 delay 0 .05 .1 .15 .2 .25 remix - fade 0 4 .1 norm -1

Convert a PDF file to JPEG:

pdftocairo path/to/file.pdf -jpeg

Tally a column of figures in awk:

awk '{sum += $3} END {print "Total:", sum}' tribes

Tell Python to recite the powers of two in English

seq 1 16|while read n;do echo $((2**n));done|python3 -c "import sys,inflect;p=inflect.engine();[print(p.number_to_words(int(x.strip()))) for x in sys.stdin if x.strip()]"

Extract pages from a PDF file:

gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -dFirstPage=34 -dLastPage=34 -sOutputFile=test2.pdf The-Cambridge-Handbook-of-Physics-Formulas.pdf

Display an image without a border:

feh --borderless /home/teresita/Downloads/48773.jpg &

Batch convert HTML to text:

for i in *.html; do lynx --dump "$i" > "${i%%.*}.txt";done

Build a list of words from a file:

cat gettysburg.txt | tr ' ' '\012'|tr '[A-Z]' '[a-z]' |sed "s/punct://g" |sort|uniq

Sort the Holy Bible by verse length with Perl:

perl -e 'print sort {length $b <=> length $a} <>' kjv.txt

Batch convert HTML to text:

for i in *.html; do lynx --dump "$i" > "${i%%.*}.txt";done

Use awk to print a count of words ending by letter:

awk 'length > 1{++a[substr(tolower($0), length)]}END{for (k in a) print a[k], k}' words.big | sort -n

Grab a stretch of unicode while inside #vim

:py3 import vim;vim.current.buffer.append(" ".join(chr(i) for i in range(945,970)))

Get a weather forecast at the console:

curl -s -L https curl wttr.in/seattle

Shuffle and rename the files in a directory of images

echo '#!/bin/bash' > files; chmod 744 files; ls -1 | shuf | nl -i1 -s' ' -nrz -w5 | awk '{print "mv " $2 " " $1 ".jpg"}' >> filesย ; ./files

Build thumbnails with the same basename from a directory of video files using ffmpegthumbnailer and bash:

for oldfile in *.mp4; do filename="${oldfile%.*}"; ffmpegthumbnailer -i "$filename.mp4" -o "$filename.jpg"; done

Calculate parallel resistance (or series capacitance) for any number of discrete components with Python

python3 -c "import sys; print(f'{1/sum(1/float(x) for x in sys.argv[1:]):.2f}')" 1800 2700 3300

 

 

 

 

 

 

Posted in Uncategorized | Leave a comment