Skip to content

Instantly share code, notes, and snippets.

View en0's full-sized avatar

Ian Laird en0

View GitHub Profile
@en0
en0 / example-usage.py
Last active December 23, 2022 05:19
Sub-process to plot a live multi-series line graph.
from collections import deque
from string import printable
from liveplot import LivePlot
# Create the LivePlot object. The lambda takes the queued data and converts into
# a Tuple consisting of the x-axis value (probably time) and a list of metrics.
# Each metric is a tuple consisting of a name for the series and a value.
plot = LivePlot(lambda counter, queue_depth: (counter, [("depth", queue_depth)]))
@en0
en0 / gzip-zlib.py
Created August 26, 2021 20:25
gzip with zlib
import zlib
compressor = zlib.compressobj(method=zlib.DEFLATED, wbits=zlib.MAX_WBITS | 16)
with open('output.txt', 'wb') as fd:
for i in range(10000):
fd.write(compressor.compress(f"Line number {i}\n".encode('utf-8')))
fd.write(compressor.flush())
@en0
en0 / eeprom_programmer.c
Created August 7, 2021 04:35
eeprom programmer
#define SHIFT_DATA 13
#define SHIFT_CLK 3
#define SHIFT_LATCH 4
#define EEPROM_D0 5
#define EEPROM_D7 12
#define WRITE_EN 2
void setAddress(int address, bool outputEnable) {
// Send address (and output-enable bit) to shift registers.
shiftOut(SHIFT_DATA, SHIFT_CLK, MSBFIRST, (address >> 8) | (outputEnable ? 0x00 : 0x80));
@en0
en0 / fib_theory.py
Created February 16, 2020 21:28
A attempt at computing the nth fib number in O(1) - didn't work of course because phi is irrational and poor approximations gets me to about fib(70) before it becomes inaccurate.
# This is not computationally accurate due to a poor
# representation of root 5
root5 = 5 ** 0.5
phi = (1 + root5) / 2
fib = lambda n: int(round((phi ** n) / root5))
fib(70)
@en0
en0 / fib_memory.py
Created July 4, 2019 21:13
Memorization Decorator
## Generic memorization wrapper
def memorize(fn):
data = {}
def _wrapped_memorize(*args):
key = hash(args)
if key in data:
return data[key]
val = fn(*args)
data[key] = val
return val
@en0
en0 / tail_recursion.py
Created July 4, 2019 20:50
Tail Recursion Optimizations in Python
class BreakOutStack(Exception):
"""Raised to clear stack frame and reset"""
def __init__(self, *args):
self.args = args
def tail_recursive(fn):
def _wrapped_tail_recursive(*args):
while True:
try:
@en0
en0 / is_prime.py
Last active November 10, 2018 16:31
O(1) Prime test
def is_prime(p):
"""Not Perfect - Do not use."""
if p > 2:
return ((2**(p-1)) % p) == 1
return p == 2
@en0
en0 / equalish.c
Created November 10, 2018 15:55
Compute equality on floating points within variance
void eqish(f1, f2) {
static float e = 1.0e-nf; // where n is the precision
return abs((f1 - f2) < e);
}
@en0
en0 / WhatSortIsThis.py
Created November 6, 2018 17:25
Late night whiteboard session yielded this sort algorithm. I am unsure what the algorithm it is called.
def sort(s):
for i in range(len(s) - 1):
j = len(s) - 1
while j > i:
if s[i] > s[j]:
s[i], s[j] = s[j], s[i]
j -= 1
return s
#!/usr/bin/env node
const fs = require("fs");
const path = process.argv.pop();
const code = fs.readFileSync(path, "utf8");
const lines = code.split("\n");
const ret = JSON.stringify(lines, null, 2);
process.stdout.write(ret);