Skip to content

Instantly share code, notes, and snippets.

View liamgriffiths's full-sized avatar

Liam Griffiths liamgriffiths

View GitHub Profile
@liamgriffiths
liamgriffiths / rotate90.scm
Created February 15, 2014 18:25
rotate a matrix 90 degrees to the right in scheme
(define (transpose m)
(apply map list m))
(define (rotate-90 m)
(transpose (reverse m)))
(define matrix
(list (list 'a 'b 'c 'd)
(list 'e 'f 'g 'h)
(list 'i 'j 'k 'l)
@liamgriffiths
liamgriffiths / timers.js
Created November 7, 2014 04:24
timers in javascript/node.js
/**
* timers in action
* http://nodejs.org/api/timers.html
*/
// lastly this one is called, because it isn't resolved on the queue until ~50ms
// long after all the other functions are queued up in the event loop
setTimeout(function() {
console.log(5);
}, 50);
@liamgriffiths
liamgriffiths / Makefile
Last active August 29, 2015 14:10
hello world nasm
# osx nasm compile using macho64
NASM=/usr/local/bin/nasm
LD=/usr/bin/ld
all: hello
@./hello
hello: hello.o
@$(LD) -macosx_version_min 10.0 -o hello hello.o
@liamgriffiths
liamgriffiths / belsum
Created April 10, 2015 05:49
more cow bel
#!/bin/bash
# hear the md5sum of a file! ♬ ♬ ♬ ♬
# by liam (github.com/liamgriffiths)
function beat {
tput bel
}
function beats {
@liamgriffiths
liamgriffiths / blimpo.cc
Last active October 2, 2015 22:48
blimp code
/*
borrowed:
- IR code/library from http://www.arcfn.com/2009/08/multi-protocol-infrared-remote-library.html
- ultrasonic sensor code from http://arduino.cc/en/Tutorial/Ping
*/
#include <IRremote.h>
#include <Servo.h>
// pins pins pins
// merge sort
// https://en.wikipedia.org/wiki/Merge_sort
var sort = function(list) {
// if list contains one element, it is sorted
if (list.length <= 1) return list;
// divide list in half into 2 sublists
var middle = Math.floor(list.length / 2);
var left = list.slice(0, middle);
// quick sort
// https://en.wikipedia.org/wiki/Quicksort
var sort = function(list) {
if (list.length <= 1) return list;
var pivot = list.splice(Math.floor(list.length / 2), 1);
var less = [], greater = [];
while (list.length) {
/**
* Binary Search Tree
*
* This is a tree data structure that orders the nodes of the tree such that:
* - The left subtree only contains nodes of lesser values
* - The right subtree only contains nodes of greater values
* - There are no duplicate nodes
*
* What this is good for:
* - Storing unique values
@liamgriffiths
liamgriffiths / set.js
Last active January 1, 2016 22:59
set data structure built on top of js object
var Set = function() {
this._items = {};
};
Set.prototype = {
values: function() {
return Object.keys(this._items);
},
insert: function(val) {
@liamgriffiths
liamgriffiths / binary_heap.js
Last active January 1, 2016 22:59
binary heap and priority queue structures
/*
* Binary Heap
*
* This is a heap which is built on top of a binary tree. The binary tree can
* be built using a regular array and some arithmetic to find the array indicies
* of a particular node's children.
*
* The Binary Heap is a 'complete tree', meaning that all levels of the tree are
* completely filled before going deeper. The levels of the tree are filled from
* left to right.