Skip to content

Instantly share code, notes, and snippets.

View wetmore's full-sized avatar

Matthew Wetmore wetmore

View GitHub Profile
@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
@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 / 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 / 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 + ",");',
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: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)
@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:11137017
Last active August 29, 2015 14:00
Fun math problems

Fun problems (rendered at http://mathb.in/18258)

  1. Find $\displaystyle\sum_{n=1}^\infty \frac{n}{2^n}$

  2. Find $\displaystyle\sum_{n=1}^\infty \frac{n^2}{2^n}$

  3. Let $a(k) = \displaystyle\sum_{n=1}^\infty \frac{n^k}{2^n}$. Find a recurrence relation for $a(k)$.

  4. Let $\mathcal{N}$ be the set of natural numbers that do not contain a 6 in their decimal expansion (so, $\mathcal{N} = { 1, 2, 3, 4, 5, 7, \ldots, 14, 15, 17, \ldots }$). Prove that $$ \sum_{n\in\mathcal{N}} \frac{1}{n} < 80 $$

  1. Give an algorithm to find the longest common subsequence for two given strings using edit distance (the minimal number of additions, deletions and substitutions needed to transform one string into the other).
@wetmore
wetmore / gist:2c6286e0873fd2ee4b7e
Created October 4, 2014 20:36
Tex source for notes on a talk about quantum teleportation
\documentclass[11pt, oneside]{article} % use "amsart" instead of "article" for AMSLaTeX format
\usepackage{geometry} % See geometry.pdf to learn the layout options. There are lots.
\geometry{letterpaper} % ... or a4paper or a5paper or ...
%\geometry{landscape} % Activate for for rotated page geometry
%\usepackage[parfill]{parskip} % Activate to begin paragraphs with an empty line rather than an indent
\usepackage{graphicx} % Use pdf, png, jpg, or eps with pdflatex; use eps in DVI mode
% TeX will automatically convert eps --> pdf in pdflatex
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{epigraph}