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 flatten(arr) { | |
return arr.reduce(function(res, x) { | |
if (typeof x.length !== 'undefined' && x.length > 0) | |
x = flatten(x); | |
return res.concat(x) | |
}, []); | |
} |
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 generateStream(len, alphabet) { | |
alphabet = alphabet || [0,1] | |
var hash = {}; | |
var str = ''; | |
for (var i=0;i<len;i++) { | |
str += alphabet[0]; | |
} | |
hash[str] = 1; | |
while (true) { | |
var currString = str.substring(str.length-len+1); |
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 myMaze = [ | |
['f', 'f', 'f', 'f', 'w', 'f', 'f', 'f', 'f', 'f'], | |
['f', 'f', 'f', 'f', 'w', 'f', 'f', 'w', 'f', 'f'], | |
['f', 'f', 'w', 'w', 'w', 'f', 'f', 'f', 'f', 'f'], | |
['w', 'w', 'w', 'f', 'w', 'f', 'f', 'w', 'w', 'f'], | |
['f', 'f', 'f', 'f', 'w', 'f', 'f', 'w', 'f', 'f'], | |
['f', 'f', 'f', 'w', 'w', 'e', 'w', 'w', 'w', 'f'], | |
['f', 'f', 'f', 'f', 'w', 'f', 'w', 'f', 'w', 'f'], | |
['f', 'f', 'f', 'f', 'w', 'f', 'w', 'f', 'w', 'f'], | |
['f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'w', 'f'], |
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 unboundedKnapsack(tuples, capacity) { | |
var result = [0]; | |
for (var i=1; i<=capacity; i++) { | |
var maxValue = tuples.reduce(function(max, curr) { | |
var val = 0; | |
var weight = curr[0]; | |
var cost = curr[1]; | |
if (cost>0 && weight<=i) { | |
if (weight==0) { | |
val = Number.POSITIVE_INFINITY; |
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 pattern(str, alphabet) { | |
alphabet = alphabet || ["0", "1"]; | |
var chars = str.split(""); | |
var result = []; | |
chars.forEach(function(char) { | |
if (result.length == 0) { | |
result = [""]; | |
} | |
var newResult = []; | |
result.forEach(function(item){ |
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 permutation(str, cache) { | |
var chars = str.split(""); | |
if (chars.length == 1) { | |
return chars; | |
} | |
cache = cache || {}; | |
if(cache[str]) { | |
return cache[str]; | |
} |
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 combination(str, n) { | |
var chars = str.split(""); | |
n = Math.min(chars.length, n); | |
var result = [[]]; | |
for (var i=0; i<n; i++) { | |
var newResult = []; | |
result.forEach(function(item) { | |
var lastIndex = -1; | |
if (item.length>0){ | |
lastIndex = item[item.length-1]; |
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 sequence(str, n) { | |
var chars = str.split(""); | |
n = n || str.length; | |
var result = [""]; | |
for (var i=0;i<n;i++) { | |
var newResult = []; | |
chars.forEach(function(char){ | |
result.forEach(function(item) { | |
newResult.push(char+item); | |
}); |
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 powerSet(str) { | |
var chars = str.split(""); | |
var result = [""]; | |
chars.forEach(function(v){ | |
var newResult = []; | |
result.forEach(function(item) { | |
newResult.push(item+v); | |
}); | |
result = result.concat(newResult); | |
}); |
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
// only support up to the n = 32 | |
// assuming Math.log() is O(1) | |
function compute(array, max){ | |
max = max || 32; | |
var sum = Math.pow(2,max)-1; | |
array.forEach(function(v){ | |
sum -= Math.pow(2,v-1); | |
}); | |
var result = []; |
NewerOlder