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 crack (hash, n) { | |
var letters = "acdegilmnoprstuw"; | |
var cracked = ''; | |
while (n>0) { | |
var mod = hash % 37; | |
hash = (hash-mod)/37; | |
if (mod < 0 || mod >= letters.length) | |
throw "invalid hash!"; | |
cracked = letters[mod] + cracked; | |
n--; |
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 LCS(s1, s2) { | |
var result = []; | |
for (var i=0; i<=s1.length; i++) { | |
result.push([]); | |
for (var j=0; j<=s2.length; j++) { | |
var currValue = 0; | |
if (i==0 || j==0) { | |
currValue = 0; | |
} else if (s1.charAt(i-1) == s2.charAt(j-1)) { | |
currValue = result[i-1][j-1] + 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 nextLex(str) { | |
var breakIndex = -1; | |
var chars = str.split(""); | |
for (var i=chars.length-1; i>0; i--) { | |
if (chars[i] > chars[i-1]) { | |
breakIndex = i-1; | |
break; | |
} | |
} |
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 LIS(arr) { | |
var parent = []; | |
var lengths = []; | |
var globalMaxIndex = 0; | |
for(var i=0; i<arr.length; i++) { | |
var currMax = -1; | |
var currLen = 0; | |
for (var j=0; j<i; j++) { | |
if (arr[j]<arr[i] && (currMax == -1 || lengths[currMax]< lengths[j])) { | |
currMax = j; |
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 = []; |
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
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 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 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 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){ |
OlderNewer