Skip to content

Instantly share code, notes, and snippets.

View mlbright's full-sized avatar

Martin-Louis Bright mlbright

  • Toronto
  • 04:44 (UTC -04:00)
View GitHub Profile
@mlbright
mlbright / slidingwindowmap.py
Created September 5, 2012 03:35
Solution to the SlidingWindowMap coding challenge by Cedric Beust
# solution to http://beust.com/weblog/2012/09/02/coding-challenge-a-sliding-window-map/
import heapq
import time
class SlidingWindowMap(object):
def __init__(self,api_keys,max_count,period_minutes):
self.period = period_minutes * 60
self.pq = []
for api_key in api_keys:
@mlbright
mlbright / dump_filter.pl
Created July 12, 2012 15:58
script to convert a MySQL 4.0.23 dump file to be usable in 5.x
#!/usr/bin/env perl
# Filters a mysql dump file to migrate from 4.0.23 to 5.x
use strict;
use warnings;
while (<>) {
s/TYPE=MyISAM/ENGINE=MyISAM/g;
s/timestamp\(14\)/timestamp/g;
@mlbright
mlbright / grayscale.go
Created January 12, 2012 02:55
how to convert a png to grayscale in the go programming language
// see http://stackoverflow.com/questions/8697095/how-to-read-a-png-file-in-gray-scale-using-the-go-programming-language
// Many thanks to Evan Shaw http://stackoverflow.com/users/510/evan-shaw
package main
import (
"image"
"image/png" // register the PNG format with the image package
"os"
#!/usr/bin/python
# Response to challenge at http://beust.com/weblog/2011/10/30/a-new-coding-challenge/
import sys
from itertools import combinations, chain
N = 40
def powerset(iterable):
"powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
s = list(iterable)
@mlbright
mlbright / subsetsumzero.py
Created April 28, 2011 20:46
Subset sum problem
# From hacker news
# http://apps.ycombinator.com/item?id=2267312
def subset_summing_to_zero(activities):
subsets = {0: []}
for (activity, cost) in activities.iteritems():
old_subsets = subsets
subsets = {}
for (prev_sum, subset) in old_subsets.iteritems():
subsets[prev_sum] = subset
new_sum = prev_sum + cost
@mlbright
mlbright / distkeys.py
Created February 23, 2011 16:25
deploy/distribute ssh public key to multiple hosts using Fabric
"""
usage:
fab -f distkeys.py set_hosts:/path/to/hosts_file add_key:/path/to/key
Use --password and --user options as necessary
Inspired by shell script at http://github.com/mlbright/mybin/raw/master/distkeys.sh
Fabric recipe to distribute your ssh key to large number of hosts.
#!/usr/bin/python
# ---------------------------------------------------------------------------- #
import sys
import os
import errno
import stat
import shutil
def _on_access_error(func, path, excinfo):
#!/usr/bin/env python
# anagram checker
from collections import defaultdict
d = defaultdict(set)
for i in open('/usr/share/dict/words'):
i = i.strip().lower()
if len(i) == 9:
d[tuple(sorted(i))].add(i)
#!/usr/bin/python
# Brute force solution to the Facebull problem
import cProfile
import sys, os, re, operator
import dijkstra
from graph import cross
def verify(kset, combos):
#!/usr/bin/python
# --------------------------------------------------------------------------------- #
import sys, os
from graph import load, pathCost, findHamiltonianCycles as fhc
# --------------------------------------------------------------------------------- #
def machineList(path,names):
machines = []