This file contains 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
mmultiply = function(a,b) { | |
return a.map(function(x,i) { | |
return transpose(b).map(function(y,k) { | |
return dotproduct(x, y) | |
}); | |
}); | |
} | |
dotproduct = function(a,b) { | |
return a.map(function(x,i) { |
This file contains 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 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 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
// O(n) | |
Array.prototype.submax = function() { | |
var max = sum = 0; | |
for(var i = 0, l = this.length; i < l; i++) { | |
sum = Math.max(sum + this[i], 0); | |
max = Math.max(max, sum); | |
} | |
return max; | |
} |
This file contains 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
calculate = function(a, f) { return a.reduce(function(x,y) { return f(x, y) }); } | |
sum = function(a, b) { return a + b; } | |
sub = function(a, b) { return a - b; } | |
sumAll = function(a) { return calculate(a, sum); } | |
subAll = function(a) { return calculate(a, sub); } |
This file contains 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 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 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 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 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; | |
}); | |
} |
NewerOlder