Skip to content

Instantly share code, notes, and snippets.

def to_sparse(p_dense):
states = numpy.indices(numpy.shape(p_dense))
p_sparse = {}
for state in states.transpose():
state = tuple(state)
probability = p_dense[state]
if probability > 0:
p_sparse[state] = p_dense[state]
return p_sparse
import cmepy.solver
def do_something_fancy(y):
"""
transforms the solution y in some interesting and useful manner
"""
return y # TODO IMPLEMENT ME
class FancySolver(object):
@fcostin
fcostin / numpy_bug.py
Created March 12, 2010 00:58
bug in numpy 1.2.1
# leads to a segfault in version 1.2.1
import numpy
bad = (numpy.array([0]), numpy.array(0))
numpy.asarray(bad)
@fcostin
fcostin / quine_1.py
Created March 18, 2010 04:57
first attempt at a python3 quine
b = ['b = ', "print('%s' % str(x[0]), end='')", '\n', 'def f(*x): eval(b[1]) if type(x[0]) is str else [g(s) for s in x]', "def g(x): print('f(b[%d])' % x if type(x) is int else 'f(%s)' % x)", "0, 'b', 4, 2, 3", '2, b[5], b[6]']
def g(x): print('f(b[%d])' % x if type(x) is int else 'f(%s)' % x)
def f(*x): eval(b[1]) if type(x[0]) is str else [g(s) for s in x]
f(b[0])
f(b)
f(b[4])
f(b[2])
f(b[3])
f(b[2])
f(0, 'b', 4, 2, 3)
@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)
@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 / 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 / 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 / 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 / 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])