This file contains hidden or 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 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 |
This file contains hidden or 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_display(): | |
def naturals(): | |
i = 0 | |
while True: | |
yield i | |
i += 1 | |
i = iter(naturals()) | |
def display(a, b): |
This file contains hidden or 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 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(): |
This file contains hidden or 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
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: |
This file contains hidden or 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
""" | |
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]) |
This file contains hidden or 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
on ubuntu... | |
sudo apt-get install xsel | |
then from inside vim to copy the contents of the current file to clipboard | |
! xsel -b < % |
This file contains hidden or 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 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: |
This file contains hidden or 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
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) |
This file contains hidden or 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
""" | |
Plot x,y pairs from csv file. | |
usage: | |
python plot_csv.py xy_data.csv | |
csv format: | |
assume no header row |
This file contains hidden or 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
a = "for x in ('a = '+repr(a), a): print(x)" | |
for x in ('a = '+repr(a), a): print(x) |