Skip to content

Instantly share code, notes, and snippets.

View senderle's full-sized avatar

Jonathan Scott Enderle senderle

View GitHub Profile
@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 / 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 / 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 / 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 / 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
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 / mem.py
Created November 6, 2015 19:40
An extremely simple object-size checker that can handle anything returned by JSON.
from collections import Sequence, Mapping
from sys import getsizeof
def traverse(obj):
if isinstance(obj, basestring):
yield obj
elif isinstance(obj, Sequence):
yield obj
for o in obj:
for inner_o in traverse(o):
@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
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@senderle
senderle / plato-perseus-english.txt
Last active October 31, 2017 15:45
All English translations of works by Plato available from the Perseus Project (http://www.perseus.tufts.edu/hopper/opensource/download).
An Athenian Stranger Clinias of Crete Megillus of Lacedaemon
Athenian To whom do you ascribe the authorship of your legal arrangements, Strangers? To a god or to some man? Clinias To a god, Stranger, most rightfully to a god. We Cretans call Zeus our lawgiver; while in Lacedaemon , where our friend here has his home, I believe they claim Apollo as theirs. Is not that so, Megillus? Megillus Yes. Athenian Do you then, like Homer, Cp. Hom. Od. 19.178 . say that
Minos used to go every ninth year to hold converse with his father Zeus, and that he was guided by his divine oracles in laying down the laws for your cities? Clinias So our people say. And they say also that his brother Rhadamanthys,—no doubt you have heard the name,—was exceedingly just. And certainly we Cretans
would maintain that he won this title owing to his righteous administration of justice in those days. Athenian Yes, his renown is indeed glorious and well befitting a son of Zeus. And, since you and our friend Megillus were bo