Skip to content

Instantly share code, notes, and snippets.

View wetmore's full-sized avatar

Matthew Wetmore wetmore

View GitHub Profile
from django.conf import settings
from django.db import models
# A Batch represents one batch, and may be assigned to multiple
# users.
class Batch(models.Model):
name = models.CharField(max_length=200)
assigned_users = models.ManyToManyField(settings.AUTH_USER_MODEL, blank=True)
def __str__(self):
{-# LANGUAGE GADTs, RankNTypes, TypeFamilies, DataKinds, DeriveFunctor, TypeOperators #-}
import Data.Type.Equality
import Data.List (intercalate)
import Data.Maybe (catMaybes)
data HasVars = Var | NoVar
data SHasVars a where
SVar :: SHasVars Var
@wetmore
wetmore / jsreview.md
Last active December 29, 2015 03:49
Reviewing some parts of javascript I don't know well enough. And also just a dump of some current knowledge.
@wetmore
wetmore / gist:6600824
Last active December 23, 2015 07:29
hack101 api
import requests
import os
twilio_sid = os.environ['TWILIO_SID']
twilio_auth = os.environ['TWILIO_AUTH']
twilio_url = 'https://api.twilio.com/2010-04-01/Accounts/%(twilio_sid)s/SMS/Messages.json' % locals()
weather_url = 'http://api.openweathermap.org/data/2.5/weather'
weather_params = {'q': 'montreal'}
r = requests.get(weather_url, params=weather_params)
from random import randint
def run(goal, runs):
total = 0
for x in xrange(runs):
sequence = []
for i in xrange(3):
sequence.append(randint(0,1))
while sequence[-3:] != goal:
sequence.append(randint(0,1))
@wetmore
wetmore / gist:6011927
Created July 16, 2013 19:38
node.js quine (inspired by the java example on wikipedia)
var quote = String.fromCharCode(39);
var source = [
'var quote = String.fromCharCode(39);',
'var source = [',
' ',
'];',
'console.log(source[0]);',
'console.log(source[1]);',
'source.forEach(function(line) {',
' console.log(source[2] + quote + line + quote + ",");',
@wetmore
wetmore / 2013-01-09-Analysis2.md
Created January 9, 2013 17:57
Some notes from analysis

Proposition: If $f : A \to \mathbb{R}$ is continuous at $c \in A$, $kf$ is continuous at $c$ for $k \in \mathbb{R}$ Proof: Let $\epsilon > 0$ be given. Since $f$ is continuous at $c$, $\exists \delta > 0$ such that if $|x - c| < \delta$ then $|f(x) - f(c)| < \epsilon / (|k| + 1)$. Thus if $|x - c| < \delta$:

$$ |kf(x) - kf(c)| = |k||f(x) - f(c)| \leq \frac{|k|\epsilon}{|k| + 1} = \frac{|k|}{|k|+1}\epsilon < \epsilon $$

If we want to show that the product of two continuous functions is continuous, we need the following lemma:

Lemma: If $f : A \to \mathbb{R}$ is continuous at $c \in A$, $f$ is bounded in a neighborhood of $c$. Proof: Let $\epsilon = 1$. As $f$ is continuous at $c$ we know there exists some $\delta &gt; 0$ such that if $|x - c| &lt; \delta$, then $|f(x) _ f(c)| &lt; \epsilon = 1$. By the triangle inequality, we can go on to say that $|f(x)| - |f(c)| \leq |f(x) - f(c)| &lt; 1$, therefore $|f(x)| &lt; 1 + |f(c)| = M$. Thus if $|x - c| &lt; \delta$, then $|f(x)| &lt; M$, i.e. it is bounded.

@wetmore
wetmore / stocks.hs
Last active November 8, 2015 08:16
Find the maximum profit from buying and selling once.
data StockState = StockState { hi :: Int, lo :: Int, lowest :: Int }
deriving Show
solve xs = (hi st) - (lo st)
where
st = solve' xs
solve' [] = StockState 0 0 0
solve' (x:xs) = foldl go (StockState 0 0 x) xs
where
@wetmore
wetmore / cosine-sim.js
Created July 30, 2012 03:52
Cosine similarity calculator
var _ = require('underscore');
var freqVector = function(word, letters) {
var freq = _.groupBy(word.split(''), function(l) {return l;});
return _.map(letters, function(l) {
return freq[l] ? freq[l].length : 0;
});
};
var dot = function(v1, v2) {
@wetmore
wetmore / gist:2956582
Created June 19, 2012 21:16
Pattern to execute a callback after a function is called a certain number of times
function counter(times, callback) {
var ct = 0;
return function() {
ct++;
if (ct === times)
callback();
};
};
// register a counter function