Skip to content

Instantly share code, notes, and snippets.

@gigamonkey
gigamonkey / fn-to-svg.py
Created Oct 8, 2021
This is how I roll ...
View fn-to-svg.py
#!/usr/bin/env python
from math import cos, pi
def svg(width, height, s, fn):
zero = s(0)
points = " ".join(f"{x},{s(fn(x, width))}" for x in range(width))
View groups.py
#!/usr/bin/env python
"Group people so everyone meets everyone."
import sys
from argparse import ArgumentParser, FileType
from collections import defaultdict
from itertools import combinations
from random import choice
@gigamonkey
gigamonkey / mult.py
Created May 15, 2020
Got nerd sniped by a terrible interview question
View mult.py
#!/usr/bin/env python3
# Let's pretend we have to implement 32-bit multiplication with just
# addition, subtraction, equality/inequality tests, and bit twiddling
# (shifts and bitwise logical ops). For bonus points detect overflow
# and signal via an exception.
# A friend got asked this on an interview. It is a terrible interview
# question unless possibly you're interviewing someone to be a junior
# chip designer or something. But it nerd sniped me anyway as an
View build_monorepo
#!/bin/bash
set -x
set -e
here=$(dirname $(realpath "$0"))
dir="$1"
mirrors="$2"
@gigamonkey
gigamonkey / steele-on-proofs.md
Last active Mar 26, 2019
Guy Steele on formal proofs from Coders at Work
View steele-on-proofs.md

Guy Steele on formal proofs from Coders at Work

Seibel: Do you ever try to formally prove your code correct?

Steele: Well, it depends on the code. If I’m writing code with some kind of tricky mathematical invariant I will go for a proof. I wouldn’t dream of writing a sorting routine without constructing some kind of invariant and proving it.

Seibel: Peter van der Linden in his book Expert C Programming has a sort of dismissive chapter about proofs in which he shows a proof of something, but then, ha ha, this proof has a bug in it.

Steele: Yes, indeed. Proofs can also have bugs.

View parens.py
#!/usr/bin/env python3
"Generate all strings containing just ( and ) with parens balenced."
from itertools import count
def parens():
def nested(x):
return ("(" * x) + (")" * x) if x > 0 else ""
View levenshtein.py
def levenshtein_distance(a, b):
"""
Compute the Levenshtein distance between a and b.
(https://en.wikipedia.org/wiki/Levenshtein_distance) Uses an
efficient dynamic programming implementation.
"""
m = [ [ 0 for _ in range(len(a) + 1) ] for _ in range(len(b) + 1) ]
for i in range(1, len(a) + 1):
@gigamonkey
gigamonkey / tidy-branches
Last active Feb 1, 2018
Bash script for cleaning up local branches that have been removed from remotes
View tidy-branches
#!/bin/bash
# Prune remote branches that no longer exist on remotes
for remote in $(git remote); do
git remote prune "$remote"
done
# Delete branches whose tracking branch no longer exists
for b in $(git show-ref --heads | cut -c 53-); do
remote=$(git config "branch.$b.remote")
View butler.py
#!/usr/bin/env python3
#
# Dump all the messages from a given Slack channel since a certain
# timestamp, one line per message with timestamp and user name.
#
from datetime import datetime
from pytz import timezone, utc
View canonical.py
#!/usr/bin/env python3
#
# Find the best matches between a set of dirty names and a canonical set.
#
# Is O(N^2) in the number of names.
#
import json
from lcs import lcs