Skip to content

Instantly share code, notes, and snippets.

View lonnen's full-sized avatar
:shipit:

Lonnen lonnen

:shipit:
View GitHub Profile
@lonnen
lonnen / UndirectedGraph.py
Created November 13, 2010 15:24
An empty undirected graph object
class undirected_graph(dict):
"""An undirected graph edge list
In an undirected graph edges are symmetric, so vertex order doesn't matter.
Store lists or dictionaries to represent attributes.
>>> a = undirected_graph()
>>> a[4,5] = True
>>> a[5,4]
True
@lonnen
lonnen / juggling.py
Created November 18, 2010 16:48
An O(n) way to cycle a list. Inspired by Jon Bentley's 'Programming Pearls' in Chapter 2, 'Aha! Algorithms', section 2.3, 'Power of Primitives'. Less efficient than the three rotate method by a constant factor, but still interesting.
from fractions import gcd
def rotate(vector, k):
''' rotate the vector to the left '''
n = len(vector)
for i in range(gcd(k,n)):
temp = vector[i]
j = i
while True:
p = j + k
@lonnen
lonnen / parens.py
Created November 19, 2010 16:02
Algorithm P from Knuth's the Art of Computer Programming v.4 implemented in python.
'''Parens
Enumerate nested parenthesis.
Implements Algorithm P from Knuth's the Art of Computer Programming v. 4
directly with minimal optimizations for python.
Usage: python parens.py number
'''
import sys
@lonnen
lonnen / printKParens.py
Created November 20, 2010 03:03
Prints all sets of nested parenthesis of order [number] using dynamic programming.
'''
Prints all sets of nested parenthesis of order [number]
Usage: python printKParens.py number
'''
import sys
def printKParens(s, index, k_remaining):
if(index == len(s)):
print ''.join(s)
@lonnen
lonnen / ca.py
Created December 13, 2010 03:38
Processing.py demo of 1D Cellular Automata
'''ca.py
Wolfram Cellular Automata by Chris Lonnen
Demonstration of Processing.py features (in both the traditional and Microsoft
sense) in the form of a Wolfram 1-dimensional cellular automata.
Compare to Shiffman's pure Processing implementation:
http://processing.org/learning/topics/wolfram.html
'''
// simple.pde
void setup() {
size(400, 400);
stroke(255);
}
void draw() {
background(192, 64, 0);
line(150, 25, mouseX, mouseY);
# simple.py
def setup():
size(400, 400)
stroke(255)
def draw():
background(192, 64, 0)
line(150, 25, mouseX, mouseY)
@lonnen
lonnen / tiple.js
Created December 20, 2010 16:43
Searches for a triplet of values in an array that sum to the target value. Returns null if such a triplet doesn't exist. Takes O(n^2) time.
function threeTuple( array, target ) {
if (isNaN(target)){return null;}
array.sort();
for (var i = 0; i < array.length; i++) {
var j = i+1;
var k = array.length - 1; // start at the end of the array
while(k > j) {
summation = array[i] + array[j] + array[k];
if (summation === target) {return [i,j,k];}
if (summation > target) {k--;}
@lonnen
lonnen / faster_triple.js
Created December 21, 2010 20:47
Searches for a triplet of values in an array that sum to the target value. Returns null if such a triplet doesn't exist.
function findClosest(items, value){
// a modified binary search that returns the last
// index checked, whether or not it had the correct value
var startIndex = 0;
var stopIndex = items.length - 1;
var middle = Math.floor((stopIndex + startIndex)/2);
while(items[middle] != value && startIndex < stopIndex){
middle = Math.floor((stopIndex + startIndex)/2);
if (value < items[middle]){
@lonnen
lonnen / autoconf213.rb
Created January 4, 2011 22:15
Homebrew recipe for autoconf213
require 'formula'
class Autoconf213 <Formula
url 'http://ftp.gnu.org/pub/gnu/autoconf/autoconf-2.13.tar.gz'
md5 '9de56d4a161a723228220b0f425dc711'
homepage 'http://www.gnu.org/software/autoconf/'
def keg_only?
:provided_by_osx
end