This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def is_prime(p): | |
"""Not Perfect - Do not use.""" | |
if p > 2: | |
return ((2**(p-1)) % p) == 1 | |
return p == 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void eqish(f1, f2) { | |
static float e = 1.0e-nf; // where n is the precision | |
return abs((f1 - f2) < e); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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); |
NewerOlder