Skip to content

Instantly share code, notes, and snippets.

@mattmazzola
Created January 23, 2015 01:30
Show Gist options
  • Save mattmazzola/92edbd412c66187eeb3f to your computer and use it in GitHub Desktop.
Save mattmazzola/92edbd412c66187eeb3f to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
function insertNumberAtEachIndex(a, xs) {
console.log("insertNumberAtEachIndex(" + a + ", " + JSON.stringify(xs) + ") =>");
var insertions = xs.map(function (x, i, xs) {
var insertion = [].concat(xs);
insertion.splice(i,0,a);
return insertion;
});
var lastInsertion = [].concat(xs);
lastInsertion.push(a);
insertions.push(lastInsertion);
console.info(insertions);
return insertions;
}
insertNumberAtEachIndex(1, [2,3]);
insertNumberAtEachIndex(1, [2,3,4]);
function getPermutations(xs) {
console.log("getPermutations(" + JSON.stringify(xs) + ") =>");
var head
, tail
, tailPermutations
, permutations
, result
;
if(xs.length === 0) {
result = [];
}
else if(xs.length === 1) {
result = [xs];
}
else if(xs.length === 2) {
result = [[xs[0],xs[1]],[xs[1],xs[0]]];
}
else if(xs.length > 2) {
head = xs[0];
tail = xs.slice(1);
tailPermutations = getPermutations(tail);
permutations = tailPermutations.reduce(function (a, ps) {
var permutations2 = insertNumberAtEachIndex(head, ps);
return a.concat(permutations2);
}, []);
result = permutations;
}
console.log(result);
return result;
}
getPermutations([1]);
getPermutations([1,2]);
getPermutations([1,2,3]);
getPermutations([1,2,3,4]);
</script>
<script id="jsbin-source-javascript" type="text/javascript">function insertNumberAtEachIndex(a, xs) {
console.log("insertNumberAtEachIndex(" + a + ", " + JSON.stringify(xs) + ") =>");
var insertions = xs.map(function (x, i, xs) {
var insertion = [].concat(xs);
insertion.splice(i,0,a);
return insertion;
});
var lastInsertion = [].concat(xs);
lastInsertion.push(a);
insertions.push(lastInsertion);
console.info(insertions);
return insertions;
}
insertNumberAtEachIndex(1, [2,3]);
insertNumberAtEachIndex(1, [2,3,4]);
function getPermutations(xs) {
console.log("getPermutations(" + JSON.stringify(xs) + ") =>");
var head
, tail
, tailPermutations
, permutations
, result
;
if(xs.length === 0) {
result = [];
}
else if(xs.length === 1) {
result = [xs];
}
else if(xs.length === 2) {
result = [[xs[0],xs[1]],[xs[1],xs[0]]];
}
else if(xs.length > 2) {
head = xs[0];
tail = xs.slice(1);
tailPermutations = getPermutations(tail);
permutations = tailPermutations.reduce(function (a, ps) {
var permutations2 = insertNumberAtEachIndex(head, ps);
return a.concat(permutations2);
}, []);
result = permutations;
}
console.log(result);
return result;
}
getPermutations([1]);
getPermutations([1,2]);
getPermutations([1,2,3]);
getPermutations([1,2,3,4]);
</script></body>
</html>
function insertNumberAtEachIndex(a, xs) {
console.log("insertNumberAtEachIndex(" + a + ", " + JSON.stringify(xs) + ") =>");
var insertions = xs.map(function (x, i, xs) {
var insertion = [].concat(xs);
insertion.splice(i,0,a);
return insertion;
});
var lastInsertion = [].concat(xs);
lastInsertion.push(a);
insertions.push(lastInsertion);
console.info(insertions);
return insertions;
}
insertNumberAtEachIndex(1, [2,3]);
insertNumberAtEachIndex(1, [2,3,4]);
function getPermutations(xs) {
console.log("getPermutations(" + JSON.stringify(xs) + ") =>");
var head
, tail
, tailPermutations
, permutations
, result
;
if(xs.length === 0) {
result = [];
}
else if(xs.length === 1) {
result = [xs];
}
else if(xs.length === 2) {
result = [[xs[0],xs[1]],[xs[1],xs[0]]];
}
else if(xs.length > 2) {
head = xs[0];
tail = xs.slice(1);
tailPermutations = getPermutations(tail);
permutations = tailPermutations.reduce(function (a, ps) {
var permutations2 = insertNumberAtEachIndex(head, ps);
return a.concat(permutations2);
}, []);
result = permutations;
}
console.log(result);
return result;
}
getPermutations([1]);
getPermutations([1,2]);
getPermutations([1,2,3]);
getPermutations([1,2,3,4]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment