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 / flatten_array.js
Created May 13, 2016 14:50
Flatten nested arrays
function flatten(arr) {
return arr.reduce(function(res, x) {
if (typeof x.length !== 'undefined' && x.length > 0)
x = flatten(x);
return res.concat(x)
}, []);
}
@alexislagante
alexislagante / minimum_unlock_stream.js
Created December 3, 2015 06:49
Get minimum length string to break a lock
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);
@alexislagante
alexislagante / mazeSolver.js
Created November 11, 2015 07:33
Maze Solver
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'],
@alexislagante
alexislagante / unboundedKnapsack.js
Created November 8, 2015 09:34
Unbounded Knapsack
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;
@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){
@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 / 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 / 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 / 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 / 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 = [];