Skip to content

Instantly share code, notes, and snippets.

Tom Stuart tomstuart

View GitHub Profile
View setting-up-macos.md
@tomstuart
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 = Array.new(haystack.length.succ, 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 =
distances[haystack_index.succ].succ,
View a-bit-more-algebra.md

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
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)
if length.zero?
[[]]
else
array.each_index.map { |i| array.drop(i) }.flat_map do |head, *tail|
combinations(tail, length - 1).map do |combination|
[head] + combination
end
@tomstuart
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'
API_ENDPOINT = 'https://codeship.com/api/v1/projects.json'
API_KEY = ENV['CODESHIP_API_KEY'] ||
raise('set CODESHIP_API_KEY first! get it from https://codeship.com/user/edit#user_api_key')
TRIES = 5
View free-prescriptions.feature
#http://www.nhs.uk/NHSEngland/Healthcosts/Pages/Prescriptioncosts.aspx
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
SP:
@tomstuart
tomstuart / bwt.rb
Created Aug 9, 2014
An implementation of the Burrows–Wheeler transform that satisfies https://github.com/zetter/burrows-wheeler-transform
View bwt.rb
class BWT
def encode(string)
chars = string.chars + ['$']
chars.each_index.map(&chars.method(:rotate)).sort.map(&:last).join
end
def decode(string)
chars = string.chars
chars.inject([]) { |table| chars.zip(table).sort }.
map(&:join).detect { |s| s.end_with?('$') }.chop
@tomstuart
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 'https://rubygems.org/'
$ time ruby -e 'puts "Hello world"'
Hello world
real 0m0.197s
user 0m0.117s
sys 0m0.073s
You can’t perform that action at this time.