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 __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. |
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 __future__ import division | |
import random | |
import operator as op | |
import matplotlib.pyplot as plt | |
# globals | |
fig, ax = plt.subplots() | |
ax.set_aspect('equal') |
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 sys | |
from scipy.misc import comb | |
sys.setrecursionlimit(2**31-1) | |
def gcd(x, y): | |
while y: | |
x, y = y, x % y | |
return x |
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 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): |
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
#lang racket | |
(require racket/match) | |
(define (eval expr env) | |
(cond | |
[(symbol? expr) (second (assoc expr env))] | |
[(number? expr) expr] | |
[(list? expr) | |
(match expr |
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 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): |
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 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,) |
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 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)] |