Skip to content

Instantly share code, notes, and snippets.

View senderle's full-sized avatar

Jonathan Scott Enderle senderle

View GitHub Profile
from timeit import timeit
from random import choice, randint
from string import ascii_letters
# random_keys = [''.join([choice(ascii_letters) for i in range(randint(4,30))]) for i in range(10000)]
def add2dict(random_keys):
s = {}
for key in random_keys:
s[key] = 1
@senderle
senderle / citesim.py
Last active August 29, 2015 14:21
Citation Simulator
# A Citation Simulator that tries to replicate academic citation
# patterns. Using pypy is recommended. MIT License.
import collections
import random
import argparse
class Article(object):
def __init__(self, journal, year):
self.journal = journal
@senderle
senderle / sim_test.py
Last active August 29, 2015 14:18
A test to verify that the browsing similarity formula I derived (http://www.lagado.name/blog/?p=126) works
import random
import math
import collections
# This gist tests my derivation of browsing similarity.
# The browsing similarity determines the probability of moving
# from a node of type one to another node of type one in a
# fully-connected weighted bipartite network, given that the
# weights are interpreted as transition probabilities.
@senderle
senderle / indexed_function_application_test.py
Created February 12, 2015 02:22
Test code for a function that applies functions in a sequence over arbitrary selections from an array.
import timeit
setup = '''
import numpy as np
def apply_indexed_fast(array, func_indices, func_table):
func_argsort = func_indices.argsort()
func_ranges = list(np.searchsorted(func_indices[func_argsort], range(len(func_table))))
func_ranges.append(None)
out = np.zeros_like(array)
@senderle
senderle / binomial_tree.py
Last active August 29, 2015 14:14
A simple binomial tree generator written as a solution to the first Theory Problem in Tim Roughgarden's class Algorithms, Part 1 on Coursera.
import random
import sys
import itertools
# This creates a binomial tree in O(n) time, but using O(n) extra space.
# It's also possible to create an in-place binomial tree in O(n log n)
# time using O(1) extra space.
# Each tree here is represented by one leaf and zero or more subtrees
# stored in a list. The leaf is at tree[0]; each following item in the
@senderle
senderle / simpleparser.py
Last active August 29, 2015 14:06
The simplest possible functional argument parser in Python
# copyright Scott Enderle 2014 -- CC BY-SA 3.0
# http://creativecommons.org/licenses/by-sa/3.0/
# https://gist.github.com/senderle/47bf0b2b13112cf5851b
import sys
def parse_args(names_types_defaults):
'''
Takes a list of options and parses sys.argv:
@senderle
senderle / tarjans_scc.py
Last active December 16, 2015 16:09
Tarjan's Strongly Connected Components Algorithm
# Scott Enderle
# Originally posted at http://stackoverflow.com/a/6575693/577088
from itertools import chain
from collections import defaultdict
class Graph(object):
def __init__(self, edges, vertices=()):
edges = list(list(x) for x in edges)
self.edges = edges