Skip to content

Instantly share code, notes, and snippets.

View alexislagante's full-sized avatar

Alexis Lagante alexislagante

  • Toronto, Ontario, Canada
View GitHub Profile
@alexislagante
alexislagante / fogcreek_cracker.js
Last active April 9, 2017 17:24
This is a solution for the coding challenge included in this job posting from Fog Creek Software: http://www.fogcreek.com/jobs/dev/
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--;
@alexislagante
alexislagante / lcs.js
Created November 2, 2015 15:53
Longest common subsequence in Javascript
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;
@alexislagante
alexislagante / nextLex.js
Last active September 6, 2016 09:48
Next String (using counting sort)
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;
}
}
@alexislagante
alexislagante / LIS.js
Created November 4, 2015 15:30
Longest Increasing Subsequence
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;
@alexislagante
alexislagante / getmissing.js
Last active November 5, 2015 15:54
Get Missing Numbers
// 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 = [];
@alexislagante
alexislagante / powerSet.js
Created November 7, 2015 06:30
Generate the powerset of a given string
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);
});
@alexislagante
alexislagante / sequence.js
Last active November 7, 2015 08:19
Generate sequences of a string
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);
});
@alexislagante
alexislagante / combination.js
Created November 7, 2015 08:10
Get all possible combinations with length n of string
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];
@alexislagante
alexislagante / permutation.js
Created November 7, 2015 10:05
Generate all possible permutations of a string
function permutation(str, cache) {
var chars = str.split("");
if (chars.length == 1) {
return chars;
}
cache = cache || {};
if(cache[str]) {
return cache[str];
}
@alexislagante
alexislagante / Pattern combination
Created November 7, 2015 10:30
Get all possible combinations of a pattern (? is replacable by an alphabet)
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){