Skip to content

Instantly share code, notes, and snippets.

View dwf's full-sized avatar

David Warde-Farley dwf

View GitHub Profile
@dwf
dwf / gist:300966
Created February 10, 2010 23:19
Some string tokenization example code in C, written for a systems programming tutorial.
/* Demonstrates breaking up a string in much the same manner as is done by
* the standard library function strtok. It's also an actually practical
* occurrence of a triple pointer (char ***).
*
* Prepared for tutorial purposes by David Warde-Farley, CSC209H Winter 2008
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@dwf
dwf / gist:300967
Created February 10, 2010 23:21
Demonstration on the use and behaviour of fgets(), written for a tutorial.
/*
* Demonstrates the use of fgets() for reading in input, and it's behaviour
* with respect to terminating NULL characters, newlines, etc.
*
* Prepared for tutorial purposes by David Warde-Farley, CSC209H Winter 2008
*/
#include <stdio.h>
#define BUFSIZE 5
@dwf
dwf / gist:300985
Created February 10, 2010 23:30
Demonstrates the use of strchr(), written for a tutorial.
/*
* Demonstrates the use of strchr to count the number of spaces in a string
* entered by the user.
*
* Prepared for tutorial purposes by David Warde-Farley, CSC209H Winter 2008
*/
#include <stdio.h>
#include <string.h>
#!/bin/bash
# screencap2dropbox.sh -- Takes a screenshot with Mac OS X's
# 'screencapture' utility, places the output in a publicly accessible
# Dropbox folder ( http://www.dropbox.com/ for more information )
# and then copies the URL to the clipboard.
#
# By David Warde-Farley, March 18, 2010. Inspired by an earlier Automator
# version by Andrew Louis.
#
@dwf
dwf / patches.py
Created April 14, 2010 19:49
Some patch extraction code I'm using to process images.
import os
import numpy as np
import scipy.ndimage as ndimage
import matplotlib
import matplotlib.pyplot as plt
def frac_eq_to(image, value=0):
return (image == value).sum() / float(np.prod(image.shape))
def extract_patches(image, patchshape, overlap_allowed=0.5, cropvalue=None,
@dwf
dwf / gist:377466
Created April 24, 2010 04:35
And *that* is how to reverse a linked list. I'm an idiot for messing this up.
class Node(object):
def __init__(self, value):
self.value = value
self.next = None
def __str__(self):
"""Return a string representation of the list starting here."""
values = []
curr = self
while curr is not None:
@dwf
dwf / gist:468955
Created July 9, 2010 02:34
Quick hack to get pudb to respond to the %debug magic in IPython 0.10.
--- iplib.bak 2010-07-08 22:30:10.000000000 -0400
+++ iplib.py 2010-07-08 22:31:09.000000000 -0400
@@ -1684,6 +1684,14 @@
if Debugger.has_pydb:
from pydb import pm
else:
+ try:
+ import pudb
+ pudb.post_mortem((sys.last_type,
+ sys.last_value,
@dwf
dwf / gist:544590
Created August 23, 2010 01:39
Messing around with Kaprekar's procedure.
def kaprekar(s, t=4):
"""
Demonstrate Kaprekar's procedure and its convergence to Kaprekar's constant.
"""
s = str(s)
# t = 3 or 4 will converge; otherwise all bets are off.
assert len(s) <= t
if len(s) < t:
s = ('0' * (t - len(s))) + s
print s
@dwf
dwf / rocarea.py
Created August 30, 2010 19:01
ROC curve plotting code.
"""
Simple implementation of ROC curve plotting with NumPy and matplotlib.
No bells and whistles, no fancy data structures, just one function and
a (hopefully) very gentle learning curve.
"""
__author__ = "David Warde-Farley <dwf AT cs.toronto.edu>"
__copyright__ = "(c) 2010 David Warde-Farley"
__license__ = "3-clause BSD license"
@dwf
dwf / barcharts.py
Created December 3, 2010 05:35
Make some plots for our 2010 NAR Web Server paper.
"""Make some plots for our 2010 NAR Web Server paper."""
import sys
import numpy as np
import matplotlib as mpl
mpl.use('Agg') # Don't pop up figures, just render in memory
import matplotlib.pyplot as plt
# Empty dictionaries
bp = {}