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: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>
@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: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 / mixbeta.py
Created February 8, 2010 15:40
Some code (in progress) for maximum likelihood mixtures of beta distributions.
import numpy as np
import scipy.special as spec
import scipy.optimize as opt
def negative_beta_likelihood(params, sumlogd, sumlog1md, numpts):
"""
Negative log likelihood for i.i.d. beta random variables, based
on the parameters and the sufficient statistics.
params -- length-2 array of (log) parameters
@dwf
dwf / pgm.lex
Created February 1, 2010 21:13
A lexer for portable graymap (PGM) files. Use lex or flex to compile into C code.
/* Scanner that reads in a Portable Graymap (PGM) file.
*
* By David Warde-Farley -- user AT cs dot toronto dot edu (user = dwf)
* Redistributable under the terms of the 3-clause BSD license
* (see http://www.opensource.org/licenses/bsd-license.php for details)
*/
enum { PRE_START, PARSE_START, GOT_X, GOT_Y, GOT_MAX } parserstate
= PRE_START;
@dwf
dwf / funtimes.scm
Created February 1, 2010 21:10
Some simple to moderately complicated Scheme procedures I wrote while learning Scheme.
;A bunch of increasingly complex Scheme procedures I wrote while
;learning Scheme.
;
; By David Warde-Farley -- user AT cs dot toronto dot edu (user = dwf)
; Redistributable under the terms of the 3-clause BSD license
; (see http://www.opensource.org/licenses/bsd-license.php for details)
;Increment a value and return it.
(define inc
@dwf
dwf / gist:292033
Created February 1, 2010 21:07
A naive, still-sort-of-inefficient k-NN implementation in idiomatic ("vectorized") MATLAB.
function bestclass = knn(train_data, labels, example, k);
%kNN-- do k-nearest neighbours classification
%
% BESTCLASS = knn(TRAIN_DATA, LABELS, EXAMPLE, K)
%
% Takes TRAIN_DATA, a D x N matrix containing N training examples of dimension
% D; LABELS, an N-vector of the (positive integer) classes assigned to each
% column of TRAIN_DATA; EXAMPLE, a D-vector consisting of the example we
% are trying to classify; and K, the number of neighbours to use in
% classifying.
@dwf
dwf / msnlog.py
Created February 1, 2010 21:04
A script that dumps convoluted XML log files from MSN Messenger to readable plaintext.
"""
Simple script I whipped up to dump MSN Messenger logs in XML to a readable
plaintext format. It's not very robust, nor am I sure which versions of MSN
Messenger it's compatible or incompatible with; I just had a specific
conversation I wanted to read, and this was the vehicle to that end.
By David Warde-Farley -- user AT cs dot toronto dot edu (user = dwf)
Redistributable under the terms of the 3-clause BSD license
(see http://www.opensource.org/licenses/bsd-license.php for details)
@dwf
dwf / tfd.py
Created February 1, 2010 21:01
An old screen scraping RSS feed generation script for a certain webcomic.
"""
tfd.py -- Scrapes a certain set of comics' websites, and spits
out home-grown RSS feeds for them.
This is pretty useless now that all of the Sharing Machine comics
provide RSS feeds, but might serve as an okay example of old-school
screen scraping and XML generation in Python. I hear the cool kids
use BeautifulSoup (for scraping) and lxml (for XML generation)
nowadays.
@dwf
dwf / fourregions.py
Created February 1, 2010 20:57
Singhal & Wu's four regions benchmark for nonlinear classification algorithms, in Python/NumPy.
#!/usr/bin/env python
"""
NumPy implementation of the classic 'four regions' benchmark.
By David Warde-Farley -- user AT cs dot toronto dot edu (user = dwf)
Redistributable under the terms of the 3-clause BSD license
(see http://www.opensource.org/licenses/bsd-license.php for details)
"""
import numpy as np