Skip to content

Instantly share code, notes, and snippets.

View tallpeak's full-sized avatar

Aaron William West tallpeak

View GitHub Profile
@tallpeak
tallpeak / upc_checkdigit.txt
Last active August 7, 2020 17:19
UPC checkdigit calculation as a Postgresql C extension function
~/src/postgresql-12.2/src/tutorial# cat upc_checksum.c
#include "postgres.h"
#include <string.h>
#include "fmgr.h"
#include "utils/geo_decls.h"
PG_MODULE_MAGIC;
/* by value */
@tallpeak
tallpeak / c0ffee.c
Created August 21, 2020 16:02
Find words like "coffee" (#C0FFEE) which can be written as hexadecimal number.
// rewrite of Haskell to C
// from https://github.com/mitsuji/c0ffee
// Find words like "coffee" (#C0FFEE) which can be written as hexadecimal number.
// I made a couple of careless pointer errors
// Proof I should use Haskell more, and C less?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#!/usr/bin/python3
import gzip
import glob
import psycopg2
import pathlib
import time
import os
pgpass = open(os.path.expanduser("~/secrets/pgpass.txt"), "r").read()
files = []
p = os.path.expanduser("~/") # = path; add Downloads/ if necessary
-- ff is our foldingfunction
-- (c,s) is the accumulator argument, which consists of an Int and a String
-- c accumulates bits when 0 or 1 is encountered
-- when 8 bits are accumulated, they are prepended to the string s
-- ch is the next character being passed from fold.
ff (c,s) ch =
if ch=='0' || ch=='1' then
let c2=c*2+(fromEnum ch - 48) in
if c2>255 then (1,toEnum(c2-256):s)
else (c2,s)
import zio.App
import zio.console._
object HelloWorld extends App {
def decodeBindigits(bindigits : String) : String = {
var decoded = ""
var c = 1
for (ch <- bindigits) {
if (ch == '0' || ch == '1') {
c = c + c + (ch & 1)
@tallpeak
tallpeak / morse2.c
Last active August 25, 2021 03:43
Morse code encoder
// from morse.tc - 9/29/2012, 7/20/19, 8/21,22/21 - tag, sbg, lrb
#include <string.h>
#include <stdio.h>
// https://kb8ojh.net/msp430/morse_encoding.html
#define NUL 0
//#define FULLTABLE
const unsigned char morse_ascii[] = {
#ifdef FULLTABLE
NUL, NUL, NUL, NUL,
// Problem: iPhone created some files that are all 0s (NUL bytes)
// Solution: chksum each file, delete those which are all 0
// eg:
// cd "/Users/aaron/Pictures/Photos Library.photoslibrary/originals"
// find . -name "*.jp*g" -exec sh -c 'cat {} | chksum ; if [ $? -eq 0 ]; then echo {}; rm {} ; fi' \;
#include <stdio.h>
#include <stdlib.h>
int main()
@tallpeak
tallpeak / Superfile.java
Created March 8, 2022 01:17
parse a fixed-width file and convert to tab-separated (fields trimmed)
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.zip.GZIPInputStream;
public final class Superfile {
public static String SuperfileToTSV(String inputFileName, String outputFileName) {
try {
FileInputStream instream = new FileInputStream(inputFileName);
@tallpeak
tallpeak / sftocsv.rs
Created June 23, 2022 19:22
superfile to tab-separated-values
// fixedWidthToCSV
// output is short by 545 lines
// wc c:\Users\tallp\Downloads\SFA8 c:\temp\sfa8.txt
// 1167801 12474922 411065952 c:\Users\tallp\Downloads\SFA8
// 1167256 40205289 449393560 c:\temp\sfa8.txt
// 2335057 52680211 860459512 total
// https://www.youtube.com/watch?v=lLWchWTUFOQ
// Ryan Levick
// oxide.computer
module Pangram (isPangram) where
import Data.Char
import Data.Bits
charToBit :: Char -> Int
charToBit c = let bit = (fromEnum c .|. 32) - 97 in
if bit >=0 && bit < 26 then 2 ^ bit else 0
bitsum :: String -> Int