Skip to content

Instantly share code, notes, and snippets.

def collapse_intervals(intervals):
'''intervals is an iterable of 2-tuples representing the start and end of
closed intervals. returns a list of 2-tuples after collapsing any
overlapping intervals. For example:
collapse_intervals([(1,5), (9,10), (4,7)]) -> [(1,7), (9,10)]
The algorithm doesn't care if intervals are out of order, or if two points
of the same interval are out of order. It also doesn't require the points
@olooney
olooney / gist:5231480
Created March 24, 2013 11:19
ssh-copy-id lite
cat ~/.ssh/id_rsa.pub | ssh $USER@$SERVER "umask 077; test -d ~/.ssh || mkdir ~/.ssh ; cat >> ~/.ssh/authorized_keys"
# http://en.wikipedia.org/wiki/RSA_(algorithm)
p = 61
q = 53
n = p*q
totient = (p-1)*(q-1)
# here, e is 2^4 + 1; in real world applications, e is often 2^16 + 1
e = 17
"""
Example using Python's itertools and generators to implement the classic FizzBuzz problem using pythonic iterators.
Output looks like this:
$ python fizz_buzz_with_iterator.py
1
2
Fizz
@olooney
olooney / gist.py
Created December 28, 2013 02:27
simple gist API wrapper with requests module
#! /usr/bin/env python
"""
Simple wrapper around Github's gist HTTP API.
"""
import argparse
import logging
import requests
import json
from getpass import getuser, getpass
@olooney
olooney / .virmrc
Last active October 26, 2017 09:57
" Oran Looney, November 2011
""" GENERAL SECTION """
" turn on automatic syntax highlighting.
syntax on
filetype plugin on
set directory=/tmp
""" WHITESPACE """
@olooney
olooney / gp_zip_tables.sql
Created October 26, 2017 13:05
find large tables and convert them to compressed tables in Greenplum
Select
sum(size) AS total_size_on_disk,
sum(size) / (select count(*) from individual) AS per_individual
FROM (
SELECT
pg_relation_size(C.oid) AS "size"
FROM pg_class C
LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
WHERE 1=1 -- relname in (...)
ORDER BY pg_relation_size(C.oid) DESC
@olooney
olooney / xeign.cpp
Created November 30, 2017 18:35
xtensor eigenpair example
#include <iostream>
#include <vector>
#include <tuple>
#include <algorithm>
#include "xtensor/xarray.hpp"
#include "xtensor/xio.hpp"
#include "xtensor/xmath.hpp"
#include "xtensor/xrandom.hpp"
#include "xtensor/xbuilder.hpp"
@olooney
olooney / requirements.txt
Created December 11, 2017 22:33
Python 2.6.6 requirements ("pip freeze") for the pyspark environment
Markdown==2.6.10
numpy==1.6.1
ordereddict==1.2
pandas==0.14.1
patsy==0.4.1
python-dateutil==2.6.1
pytz==2017.3
requests==2.18.4
six==1.11.0
@olooney
olooney / central_limit_theorem.R
Created December 20, 2017 15:46
illustrate CLS convergence for non-skewed and highly skewed random variables.
compare_histograms <- function(sn) {
sample_means <- function(r,n) { sqrt(n)*rowMeans(matrix(r(n*10000), ncol=n)) }
center_scale <- function(v) (v - mean(v))/sd(v)
exp_sample <- center_scale(sample_means(rexp, sn))
unif_sample <- center_scale(sample_means(runif, sn))
max_break = ceiling(max(c(unif_sample, exp_sample)))
min_break = floor(min(c(unif_sample, exp_sample)))
breaks = seq(min_break,max_break,length.out=(max_break-min_break)*8+1)