This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var max = reduce(function(a, b) { return a > b; }); | |
var min = reduce(function(a, b) { return a < b; }); | |
function reduce(p) { | |
return function(a) { | |
function iter(a, c) { | |
if(a.length === 0) return c; | |
else return iter(a.slice(1), p(a[0], c) ? a[0] : c); | |
} | |
return iter(a.slice(1), a[0]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function quick_sort(a) { | |
var m = a.length; | |
if(m <= 1) return a; | |
var v = a.shift(); | |
var l = []; | |
var r = []; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Point = function(x, y) { | |
this.x = x; | |
this.y = y; | |
}; | |
function getDistance(a, b) { | |
return Math.sqrt(Math.pow((a.x - b.x), 2) + Math.pow((a.y - b.y), 2)); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function mergesort(list) { | |
if(list.length <= 1) return list; | |
var l = mergesort(list.slice(0, list.length / 2)); | |
var r = mergesort(list.slice(list.length / 2)); | |
return merge(l, r); | |
} | |
function merge(l, r) { | |
var result = []; | |
while(l.length > 0 || r.length > 0) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function opop(obj,k) { | |
for(o in obj) { | |
if(o == k) { | |
result = obj[o]; | |
delete obj[o]; | |
return result; | |
} | |
else if(typeof obj[o] == 'object' && obj[o].constructor == Object) { | |
if(a = opop(obj[o], k)) return a; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function generateSortedWorth() { | |
return options.values.map(function(x, i) { | |
return { | |
index: i, | |
worth: x / options.weights[i], | |
}; | |
}).sort(function(a, b) { | |
return a.worth > b.worth ? -1 : 1; | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function primes(n) { | |
function iter(i, acc) { | |
if(acc.length > n) return acc; | |
else if(i > 1 && divisor(2)) { | |
acc.push(i); | |
return iter(i + 1, acc); | |
} | |
else return iter(i + 1, acc); | |
function divisor(x) { | |
if(x > Math.sqrt(i)) return true; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Knapsack = function(o) { | |
this.values = o.values; | |
this.weights = o.weights; | |
this.capacity = o.capacity; | |
this.createSolutionTable = function () { | |
this.table = this.initializeMatrix(); | |
for(i = 1; i <= this.values.length; i++) { | |
for(k = 0; k < this.capacity; k++) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Node = function(o){ | |
this.key = o.key; | |
this.data = o.data; | |
this.left = {}; | |
this.right = {}; | |
} | |
var BinaryTree = function(o) { | |
this.tree = o.tree; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function enumObj(obj) { | |
function recur(obj, acc) { | |
for(var prop in obj) { | |
if(obj[prop] instanceof Object && typeof obj[prop] === 'object') { | |
acc.push(recur(obj[prop], [])); | |
} else { | |
acc.push(prop + ':' + obj[prop]); | |
} | |
} | |
return acc; |