Skip to content

Instantly share code, notes, and snippets.

@methodin
methodin / bookmarklet.html
Created August 25, 2012 17:02
Bookmarklet to support html5 drag/drop image/file password generation
<a href="javascript:(function(){var head=document.getElementsByTagName('head')[0];var e=document.createElement('script');e.src='//crypto-js.googlecode.com/svn/tags/3.0.2/build/rollups/sha256.js';head.appendChild(e);e=document.createElement('style');e.innerHTML='.picpass-active {background:#aaffaa !important;z-index:10000}';head.appendChild(e);e=document.createElement('script');e.src='//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js';head.appendChild(e);var inter=setInterval(check,100);var runs=0;function check(){if(typeof jQuery!='undefined'){run();clearInterval(inter);}if(++runs>60){clearInterval(inter);}}function run(){var dropzone=$('input[type=password]');dropzone.css({'zIndex':10000});var dropped=null;var content=null;$('body').on('dragstart',function(e){if(e.srcElement.src){content=e.srcElement.src;}});dropzone.on('dragover',function(e){e.preventDefault();$(this).addClass('picpass-active');});dropzone.on('dragleave',function(e){e.preventDefault();$(this).removeClass('picpass-active');});dropzo
@methodin
methodin / README.markdown
Created May 2, 2012 17:01 — forked from gudbergur/README.markdown
Bootstrap's Typeahead plugin extended (allowing for AJAX functionality) among other things

This is a fork of Bootstrap Typeahead that adds minimal but powerful extensions.

For example, process typeahead list asynchronously and return objects

  # This example does an AJAX lookup and is in CoffeeScript
  $('.typeahead').typeahead(
    # source can be a function
    source: (typeahead, query) ->
 # this function receives the typeahead object and the query string
@methodin
methodin / markovCluster.js
Created January 7, 2012 03:54
Markov Clustering
function round(n) {
return Math.round(n*100) / 100;
}
// Represents an edge from source to sink with capacity
var Edge = function(source, sink, capacity) {
this.source = source;
this.sink = sink;
this.capacity = capacity;
};
@methodin
methodin / mersenne-twister.js
Created January 3, 2012 06:13 — forked from banksean/mersenne-twister.js
a Mersenne Twister implementation in javascript. Makes up for Math.random() not letting you specify a seed value.
/*
I've wrapped Makoto Matsumoto and Takuji Nishimura's code in a namespace
so it's better encapsulated. Now you can have multiple random number generators
and they won't stomp all over eachother's state.
If you want to use this as a substitute for Math.random(), use the random()
method like so:
var m = new MersenneTwister();
@methodin
methodin / mersenne.js
Created January 3, 2012 06:28
Mersenne Twister
var MersenneTwister = function(seed) {
// Holds the state of the register
this.state = [seed];
this.index = 0;
for(var i=1;i<=623;i++) {
var s = this.state[i-1] ^ (this.state[i-1] >>> 30);
this.state[i] = (((((s & 0xffff0000) >>> 16) * 1812433253) << 16) + (s & 0x0000ffff) * 1812433253) + i;
this.state[i] >>>= 0;
}
@methodin
methodin / djikstra.js
Created January 3, 2012 01:29
Djikstra's Algorithm
function Djikstra(graph, source) {
this.infinity = 1.7976931348623157E+10308;
this.dist = {};
this.previous = {};
this.queue = [];
// Get the smallest distance in the queue
this.findSmallest = function() {
var min = infinity;
var smallest = null;
@methodin
methodin / graph.js
Created January 2, 2012 13:34
Graph
var Graph = function() {
this.nodes = {};
this.edges = {};
this.addNode = function(id, data) {
this.nodes[id] = data;
};
this.getNode = function(id) {
return this.nodes[id] || null;
@methodin
methodin / gale.js
Created December 30, 2011 04:35
Gale-Shipley Algorithm
var men = {
'Joe': ['Mary','Jane','Dorothy','Susan'],
'Tim': ['Jane','Dorothy','Mary','Susan'],
'Jack': ['Dorothy','Jane','Susan','Mary'],
'Matt': ['Mary','Jane','Susan','Dorothy']
};
var women = {
'Mary': ['Tim','Joe','Jack','Matt'],
'Jane': ['Matt','Jack','Joe','Tim'],
@methodin
methodin / astar.js
Created December 29, 2011 17:37
A* Path Finding
String.prototype.replaceAt=function(index, char) {
return this.substr(0, index) + char + this.substr(index+char.length);
};
var Point = function(x,y, parent) {
this.x = x;
this.y = y;
this.cost = 0;
this.moveCost = 0;
this.goalCost = 0;
this.parent = parent === undefined ? null : parent;
@methodin
methodin / sieve.js
Created December 28, 2011 19:41
Sieve of Eratosthenes
function primes(from, to) {
var arr = [];
for(var i=from;i<=to;i++) arr.push(i);
for(var check=0;check<to/2;check++) {
for(var j=check+arr[check]; j<=to,j<=arr.length-1; j+=arr[check]) {
arr.splice(j--,1);
}
}
return arr;
}