Skip to content

Instantly share code, notes, and snippets.

@jfinkels
jfinkels / statistics.py
Created January 11, 2016 04:58
Estimating mean and relative size of a sample of a population
def estimate_mean(sample, values, weights=None):
"""Estimates the average value of a set of elements based on the given
sample.
Parameters
----------
sample : iterable
Iterable of objects.
values : function
@jfinkels
jfinkels / light.py
Created September 24, 2015 20:13
Light's associativity test
# light.py - decides whether a Cayley table represents an associative operation
#
# Copyright 2015 Jeffrey Finkelstein.
#
# This file is part of Light.
#
# Light is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
@jfinkels
jfinkels / test_threesum.py
Created July 1, 2015 20:02
Solution to the 3-SUM problem in Python.
from threesum import three_sums
def test_three_sums():
integers = [2, 4, 8, 10, -7, -10, -25]
expected = [(-10, 2, 8)]
actual = list(three_sums(integers))
assert [sorted(t) for t in three_sums(integers)] == [[-10, 2, 8]]
@jfinkels
jfinkels / tails.py
Last active August 29, 2015 14:24
Iterate over all suffixes of an iterable in Python.
from itertools import islice
from itertools import tee
def tails(iterable):
iterator = iter(iterable)
while True:
tail, iterator = tee(iterator)
yield tail
# Try to pop the next element of the iterable. If there are no
@jfinkels
jfinkels / dominating_set.py
Created June 9, 2015 01:40
Approximate dominating set with postprocessing heuristic
# -*- coding: utf-8 -*-
"""
**************************************
Minimum Vertex and Edge Dominating Set
**************************************
A dominating set for a graph G = (V, E) is a subset D of V such that every
vertex not in D is joined to at least one member of D by some edge. The
import numpy as np
def GEPP(A, b, doPricing = True):
'''
Gaussian elimination with partial pivoting.
input: A is an n x n numpy matrix
b is an n x 1 numpy array
output: x is the solution of Ax=b
with the entries permuted in
@jfinkels
jfinkels / mimerenderexample.py
Created May 28, 2013 21:14
Example of why mimerender should have some way of exposing HTTP response headers and status code to renderers.
#!/bin/env python2
from flask import Flask
from flask import json
from mimerender import FlaskMimeRender
app = Flask(__name__)
app.debug = True
app.testing = True
mimerender = FlaskMimeRender()
@jfinkels
jfinkels / intaddchain.py
Last active December 14, 2015 12:19
Algorithm for solving the minimum integer addition chain problem.
#!/usr/bin/env python3
import itertools
# The input to the algorithm.
N = 11
def build_tree(n):
# This should be `limit = O(log n)`...
limit = (n // 2) + 1
@jfinkels
jfinkels / .emacs
Created February 24, 2010 04:27
My .emacs file
(global-font-lock-mode 1)
(column-number-mode 1)
(show-paren-mode 1)
(setq-default fill-column 79)
(setq default-tab-width 2)
(setq js-indent-level 2)
(setq-default indent-tabs-mode nil)
<dependency>
<groupId>jmona</groupId>
<artifactId>jmona</artifactId>
<version>0.4</version>
</dependency>