Skip to content

Instantly share code, notes, and snippets.

View joshbode's full-sized avatar
💽

Josh Bode joshbode

💽
  • Melbourne, Victoria, Australia
View GitHub Profile
library(plyr)
library(ggplot2)
formatter = function(x) formatC(x, format='d', big.mark=',')
# create some fake data
variability = c(A=0.1, B=0.2, C=0.3)
d = as.data.frame(mutate(list(),
source=sample(c('A', 'B', 'C'), 1000, replace=TRUE),
from collections import defaultdict
def splitter(l, mapper):
"""Partition an iterable by a callable mapper."""
results = defaultdict(list)
for x in l:
results[mapper(x)].append(x)
import itertools
def offsetter(iterable, offsets=(0, 1), longest=False):
"""
Return offset element from an iterable.
Pad offset element with None at boundaries.
>>> l = range(10)
>>> for prev, curr, next in offsetter(l, offsets=(-1, 0, 1), longest=True):
... print prev, curr, next
def insert_sort(l, inc=1):
"""Insert sort."""
for i, x in enumerate(l):
while x < l[i - inc] and i > 0:
l[i] = l[i - inc]
i -= inc
l[i] = x
def bubble_sort(l, inc=1):
"""Bubble Sort."""
swapped = True
while swapped:
swapped = False
for i in xrange(1, len(l)):
if l[i - inc] > l[i] and i - inc >= 0:
swapped = True
l[i], l[i - inc] = l[i - inc], l[i]
@joshbode
joshbode / merge.py
Created March 17, 2013 05:05
Merge Sort using iterators
from heapq import merge
def merge_lr(left, right):
"""Merge left and right sequences."""
result = []
left_idx, right_idx = 0, 0
while left_idx < len(left) and right_idx < len(right):
# change the direction of this comparison to change the direction of the sort
@joshbode
joshbode / tapas.r
Last active December 17, 2015 01:08
# What did Barbara get up to in Spain?
# TL;DR: 11 Gelato, 68 Drinks and 36 Tapas
library(XML)
library(ggplot2)
library(reshape2)
# parse page into XML DOM document
parse_doc = function (page) {
@joshbode
joshbode / grant_access.sql
Created August 16, 2013 22:19
Grant access to multiple people working on a project to tables with names matching a particular pattern.
-- grant access to multiple people
DECLARE
usernames VARCHAR2(1024) := '
jimmy,
bobby,
franky,
alice,
mandy
';
@joshbode
joshbode / create_table.bat
Created August 16, 2013 22:48
Create tables in a schema
@echo off
setlocal enableextensions
pushd "%~p1"
set FILENAME="%~n1"
echo @%FILENAME% exit; | sqlplus -l -s /nolog
popd
@joshbode
joshbode / csv.tex
Created August 17, 2013 01:57
Testing CSV reading into LaTeX
\documentclass{article}
\usepackage{datatool}
\usepackage{booktabs}
\usepackage{xtab}
\usepackage{longtable}
\DTLloaddb{scientists}{scientists.csv}
\begin{document}