Skip to content

Instantly share code, notes, and snippets.

View qmaurmann's full-sized avatar

Quinn Maurmann qmaurmann

  • Body Labs
  • NYC
View GitHub Profile
@qmaurmann
qmaurmann / evo.py
Created December 15, 2013 01:42
Code to produce second set of pictures (evolution of rectangles) for blog post Evolvability and Learning at qmaurmann.wordpress.com.
from __future__ import division
import random
import operator as op
import matplotlib.pyplot as plt
# globals
fig, ax = plt.subplots()
class Rect2(object):
"""Callable 2-dimensional rectangle class.
@qmaurmann
qmaurmann / rects.py
Created December 15, 2013 01:37
Code to produce first set of pictures (PAC learning for rectangles) for blog post Evolvability and Learning at qmaurmann.wordpress.com.
from __future__ import division
import random
import operator as op
import matplotlib.pyplot as plt
# globals
fig, ax = plt.subplots()
ax.set_aspect('equal')
@qmaurmann
qmaurmann / coins_cf.py
Last active June 18, 2018 14:09
Solves coin-changing problem by "compiling" closed form solutions for residue classes.
import sys
from scipy.misc import comb
sys.setrecursionlimit(2**31-1)
def gcd(x, y):
while y:
x, y = y, x % y
return x
@qmaurmann
qmaurmann / coins_dp.py
Created November 11, 2013 21:35
DP solution to coin changing problem.
def make_change(amt, denoms):
wsize = max(denoms)
d = len(denoms)
# Answer will be at window[amt % wsize][d]
window = [[0 for _ in range(d+1)] for _ in range(wsize)]
# Initialize non-zero base cases. Note window[0][0] = 0 is "incorrect",
# but this base case is not used, and left column all 0 is convenient.
for i in xrange(1, d+1):
window[0][i] = 1
for x in xrange(1, amt+1):
@qmaurmann
qmaurmann / littlelambda.rkt
Created August 24, 2013 21:26
Interpreter for a small toy language called Little Lambda for the blog post "What is a programming language?" at qmaurmann.wordpress.com/...
#lang racket
(require racket/match)
(define (eval expr env)
(cond
[(symbol? expr) (second (assoc expr env))]
[(number? expr) expr]
[(list? expr)
(match expr
@qmaurmann
qmaurmann / sam_and_polly.py
Created August 10, 2013 23:00
A solution to the Sam and Polly puzzle from http://wiki.xkcd.com/irc/Puzzles
from collections import defaultdict
SUMS = defaultdict(list)
PRODS = defaultdict(list)
for x in range(3, 98):
for y in range(x, 98):
SUMS[x+y].append((x,y))
PRODS[x*y].append((x,y))
def sam_approves(s):
@qmaurmann
qmaurmann / nqueens.py
Created July 27, 2013 18:08
A direct Python translation of the n queens solution at https://gist.github.com/qmaurmann/6095719
def n_queens(n):
def solve_first_rows(k):
if k == 0:
yield ()
else:
for partial in solve_first_rows(k-1):
for j in range(1, n+1):
if not [() for (r,c) in enumerate(partial,1)
if j == c or abs(j-c) == abs(k-r)]:
yield partial + (j,)
import Control.Monad (guard)
nQueens :: Int -> [[Int]]
nQueens n = solveFirstRows n where
solveFirstRows 0 = [[]]
solveFirstRows k | k > 0 = do
partial <- solveFirstRows (k-1)
j <- [1..n]
guard $ null [() | (r,c) <- zip [1..] partial,
j == c || abs (j-c) == abs (k-r)]