Skip to content

Instantly share code, notes, and snippets.

@fcostin
fcostin / enigma_1750.py
Created June 15, 2013 01:24
enigma number 1750 (brute force: enumerate all paths then filter)
import itertools
from collections import defaultdict
GRID_SIZE = 3
on_grid = lambda (a, b) : (0 <= a < GRID_SIZE) and (0 <= b < GRID_SIZE)
def gen_neighbours((a, b)):
for i, j in itertools.product((-1, 0, 1), repeat=2):
a_prime = a + i
def make_display():
def naturals():
i = 0
while True:
yield i
i += 1
i = iter(naturals())
def display(a, b):
@fcostin
fcostin / enumerated_casemap.py
Created November 2, 2012 04:12
enumerated_casemap
def enumerated_casemap(case_handlers, things):
i = 0
for x in things:
for p, f in case_handlers:
if p(x):
yield f(i, x)
i += 1
break
def main():
@fcostin
fcostin / adventure generator pythons
Created August 23, 2012 13:31
attack of generator.send
In [10]: def adventure():
....: print 'there is cupcake. do you eat it?'
....: x = (yield)
....: if x:
....: print 'the cupcake is tasty'
....: print 'is this awesome?'
....: x = (yield)
....: if x:
....: print 'awesome!!'
....: while True:
@fcostin
fcostin / prog21_141.py
Created June 7, 2012 07:04
python & numpy port of james hague's blog post 141
"""
python & numpy port of james hague's blog post 141
ref:
http://prog21.dadgum.com/141.html
"""
import numpy
a = numpy.array([10, 5, 9, 6, 20, 17, 1])
@fcostin
fcostin / clipboard_fffuuuuuuu.txt
Created March 21, 2012 03:53
copying to clipboard
on ubuntu...
sudo apt-get install xsel
then from inside vim to copy the contents of the current file to clipboard
! xsel -b < %
@fcostin
fcostin / gist:1977924
Created March 5, 2012 11:22
linear time partition
def exchange(a, i, j):
a[i], a[j] = a[j], a[i]
def partition(a, x):
n = len(a)
i = 0
j = 0
k = n
while j < k:
if a[j] < x:
@fcostin
fcostin / Makefile
Created February 23, 2012 14:43
test_ridge_crime.py
CRIME_DATA_URL := http://archive.ics.uci.edu/ml/machine-learning-databases/communities/communities.data
CRIME_DATA := crime.data
test: $(CRIME_DATA)
time python test_ridge_crime.py $^
.PHONY: test
$(CRIME_DATA):
wget -O $@ $(CRIME_DATA_URL)
@fcostin
fcostin / plot_csv.py
Created July 12, 2011 11:40
Plot x,y pairs from csv file.
"""
Plot x,y pairs from csv file.
usage:
python plot_csv.py xy_data.csv
csv format:
assume no header row
@fcostin
fcostin / quine_2.py
Created March 18, 2010 05:32
second python quine. nicer.
a = "for x in ('a = '+repr(a), a): print(x)"
for x in ('a = '+repr(a), a): print(x)