Skip to content

Instantly share code, notes, and snippets.

View AdamBrouwersHarries's full-sized avatar
🎼
Music, Dance & Maths

Adam Brouwers-Harries AdamBrouwersHarries

🎼
Music, Dance & Maths
View GitHub Profile
@AdamBrouwersHarries
AdamBrouwersHarries / LeftRecursionElimination
Created May 13, 2013 19:23
Eliminating left recursion from example
To begin with, the wikipedia left recursion algorithm is much eaiser to read: http://en.wikipedia.org/wiki/Left_recursion#Removing_indirect_left_recursion
So, we have the non-terminals A_1, and A_2:
A_1 -> A_2a | b
A_2 -> A_1c | ab
let's step through the algorithm:
i = 1
j = 1
@AdamBrouwersHarries
AdamBrouwersHarries / base64_encode
Created July 23, 2013 17:47
encodes a string from ascii to base64
b64_char conv_table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+\\=";
//returns a base 64 string (malloc allocated)
b64_string ascii_to_base_64(ascii_string as, int ascii_char_count)
{
//find how many ascii blocks of 3 we have (rounded up)
int three_blocks_in = (ascii_char_count/3)+(ascii_char_count%3?1:0);
//from that, how many base 64 blocks?
int characters_out = three_blocks_in*4;
//allocate b64 space
b64_string out_string = malloc(sizeof(b64_char)*(characters_out+1));
@AdamBrouwersHarries
AdamBrouwersHarries / roulette_wheel.c
Created December 19, 2013 20:40
Roulette wheel selection
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//just a quick method to print the thresholds of each of the "roulette cells"
void print_thresholds(float* random_inputs, int input_len)
{
float p_acc=0;
for (int i = 0; i < input_len; ++i)
{
printf("I:%2d L: %3f H: %3f\n",i,p_acc, p_acc+random_inputs[i]);
@AdamBrouwersHarries
AdamBrouwersHarries / Example wildcard makefile
Created January 6, 2014 18:18
An example makefile using wildcards and pattern substitution to compile a load of files of the same type
CPP_FILES = $(wildcard *.markdown)
OBJ_FILES = $(patsubst %.markdown,%.pdf,$(CPP_FILES))
%.pdf: %.markdown
pandoc -V geometry:margin=1in -S -o $@ $(patsubst %.pdf,%.markdown,$@)
all: $(OBJ_FILES)
clean:
@AdamBrouwersHarries
AdamBrouwersHarries / sparsezip.c
Created August 1, 2015 11:38
Zipping two sparse arrays
typedef struct Tuple2
{
int a;
int b;
} Tuple2;
// array lengths
int sparseLength;
int denseLength;
@AdamBrouwersHarries
AdamBrouwersHarries / mean_error_ratio.py
Created August 17, 2015 16:15
Find the error in the mean of the ratio of two means from normal distributions
from scipy.stats import t
import math
# Method from "Intuitive Biostatistics" pp. 285-286
#
#@book{harvey1995intuitive,
# title={Intuitive Biostatistics},
# author={Harvey Motulsky},
# isbn={9780195086072},
# lccn={95008166},
@AdamBrouwersHarries
AdamBrouwersHarries / arithmetic.hs
Created September 10, 2015 14:44
Super simple arithmetic expressions in haskell
{-# LANGUAGE GADTs #-}
module Main where
data Expr a where
N :: Float -> Expr Float
(:+) :: Expr Float -> Expr Float -> Expr Float
(:*) :: Expr Float -> Expr Float -> Expr Float
(:-) :: Expr Float -> Expr Float -> Expr Float
(:/) :: Expr Float -> Expr Float -> Expr Float
App :: (Float -> Float) -> Expr Float -> Expr Float
%%MatrixMarket matrix coordinate real general
%=================================================================================
%
% This ASCII file represents a sparse MxN matrix with L
% nonzeros in the following Matrix Market format:
%
% +----------------------------------------------+
% |%%MatrixMarket matrix coordinate real general | <--- header line
% |% | <--+
% |% comments | |-- 0 or more comment lines
@AdamBrouwersHarries
AdamBrouwersHarries / push.py
Created February 1, 2016 16:50
A small python script for pushbullet interaction from the command line. Very much based on the weebullet source, with some modifications.
#!/bin/python2
import sys
import json
import requests
apikey = "REDACTED"
def send_push(title, body):
print "Sending push. Title: [%s], body: [%s]\n" % (title, body)
apiurl = "https://api.pushbullet.com/v2/pushes"
def updatedict(fails, d, k, m):
f = fails
fc = "failcount"
f[d] = f.get(d, dict())
f[d][k] = f[d].get(k, dict())
f[d][k][m] = f[d][k].get(m, dict())
f[d][k][m][fc] = f[d][k][m].get(fc, 0) + 1
return f