Skip to content

Instantly share code, notes, and snippets.

View wetmore's full-sized avatar

Matthew Wetmore wetmore

View GitHub Profile
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 / 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