Skip to content

Instantly share code, notes, and snippets.

View mlbright's full-sized avatar

Martin-Louis Bright mlbright

  • Toronto
  • 00:11 (UTC -04:00)
View GitHub Profile
@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
#!/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 / 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"
@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 / 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 / socks.py
Created December 27, 2012 16:04
Calculate the expected percentage of pairs of socks recovered after randomly washing a sample
# Calculate the expected percentage of pairs of socks recovered
# after randomly washing a sample
from random import shuffle, sample
from collections import defaultdict
from time import clock
PAIRS = 20.0
EXPERIMENTS = 1000000
@mlbright
mlbright / buploader.pl
Last active December 11, 2015 16:58
upload all pictures from a given directory to Flickr, and then to Picasa Web Albums, using your secret email upload address.
#!/usr/bin/env perl
use 5.010;
use strict;
use warnings;
use Flickr::Upload;
use File::Spec;
use File::Find;
use Config::Simple;
use Path::Class;
@mlbright
mlbright / chattyrm.pl
Created March 23, 2013 02:26
Chattier version of rm -rf. Deletes every path provided on STDIN.
#!/usr/bin/env perl
use 5.010;
use strict;
use warnings;
use File::Path qw (remove_tree);
while ( my $path = <> ) {
chomp $path;
my $errors;
@mlbright
mlbright / shouldiupgradejenkins.pl
Created April 18, 2013 13:12
Should you upgrade Jenkins to the latest version? Here's the ratio of sunny ratings to the number of cloudy and stormy ratings.
#!/usr/bin/env perl
use 5.010;
use strict;
use warnings;
use HTTP::Tiny;
my $ratings = 'http://jenkins-ci.org/rate/result.php';
my $response = HTTP::Tiny->new->get($ratings);
#!/usr/bin/python
# Does this work?
# http://blog.jgc.org/2013/04/the-minimum-coin-problem-with-prize.html
coins = [200,50,50,100,100,20,20,20,10,5,5,2,2,1,1]
coins.sort()
coins.reverse()
paper = 170