Skip to content

Instantly share code, notes, and snippets.

tomstuart / levenshtein.rb
Last active Jun 15, 2017
Approximate substring matching with Levenshtein distance
View levenshtein.rb
module Levenshtein
def self.substring_distance(needle, haystack)
distances =, 0)
needle.each_char.with_index do |needle_char, needle_index|
next_distances = [needle_index.succ]
haystack.each_char.with_index do |haystack_char, haystack_index|
deletion, insertion, substitution =

By working with polynomials we can justify these definitions purely algebraically without doing any differentiation, which I hand-waved away as “a bit more algebra” in the post.

For example, from the angle sum identity

sin(ɑ + β) = sin ɑ cos β + cos ɑ sin β

we know that

sin(a + bε) = sin a cos bε + cos a sin bε

tomstuart / combinations_spec.rb
Created Jul 22, 2016 — forked from tuzz/combinations_spec.rb
Generates combinations from an array
View combinations_spec.rb
require "rspec"
def combinations(array, length)
else { |i| array.drop(i) }.flat_map do |head, *tail|
combinations(tail, length - 1).map do |combination|
[head] + combination
tomstuart / codeship
Last active Dec 14, 2015
Ruby script to open latest Codeship build for current commit
View codeship
#!/usr/bin/env ruby
require 'json'
require 'net/https'
require 'time'
raise('set CODESHIP_API_KEY first! get it from')
View free-prescriptions.feature
Given a user who is 60 or older
When they collect a prescription
Then it should be free
Given a user who is younger than 16
When they collect a prescription
Then it should be free
View calling-convention.txt
before function call: at start of function call:
ARG: args for caller args for caller
saved state for caller saved state for caller
LCL: locals for caller locals for caller
stack for caller stack for caller
args for callee --> ARG: args for callee
SP: saved state for callee
LCL: locals for callee
tomstuart / bwt.rb
Created Aug 9, 2014
An implementation of the Burrows–Wheeler transform that satisfies
View bwt.rb
class BWT
def encode(string)
chars = string.chars + ['$']
def decode(string)
chars = string.chars
chars.inject([]) { |table| }.
map(&:join).detect { |s| s.end_with?('$') }.chop
tomstuart / rules.json
Last active Aug 29, 2015
Rules for auto-explaining FreeAgent transactions
View rules.json
"textToMatch": "HISCOX",
"description": "Liability & indemnity insurance",
"vat": "0",
"category": "Insurance",
"shouldHaveAttachment": false,
"ecStatus": "Non-EC"
View gist:d9ce295b89c73a4b69ff
$ cat Gemfile
source ''
$ time ruby -e 'puts "Hello world"'
Hello world
real 0m0.197s
user 0m0.117s
sys 0m0.073s