Find the missing permutation: Difference between revisions

(→‎{{header|Perl 6}}: no need for .tree anymore)
Line 883:
 
=={{header|JavaScript}}==
 
===Imperative===
 
The permute() function taken from http://snippets.dzone.com/posts/show/1032
<lang javascript>permute = function(v, m){ //v1.0
Line 904 ⟶ 907:
missing = all.filter(function(elem) {return list.indexOf(elem) == -1});
print(missing); // ==> DBAC</lang>
 
===Functional (ES5)===
 
<lang JavaScript>(function (strList) {
 
// [a] -> [[a]]
function permutations(xs) {
return xs.length ? (
chain(xs, function (x) {
return chain(permutations(deleted(x, xs)), function (ys) {
return [[x].concat(ys).join('')];
})
})) : [[]]
}
 
// Monadic bind/chain for lists
// [a] -> f -> [a]
function chain(xs, f) {
return [].concat.apply([], xs.map(f));
}
 
// a -> [a] -> [a]
function deleted(x, xs) {
return xs.length ? (
x === xs[0] ? xs.slice(1) : [xs[0]].concat(
deleted(x, xs.slice(1))
)
) : [];
}
 
// Provided subset
var lstSubSet = strList.split('\n');
 
// Any missing permutations
return permutations('ABCD'.split('')).reduce(function (a, x) {
return lstSubSet.indexOf(x) === -1 ? a.concat(x) : a;
}, []);
 
})(
'ABCD\nCABD\nACDB\nDACB\nBCDA\nACBD\nADCB\nCDAB\nDABC\nBCAD\nCADB\n\
CDBA\nCBAD\nABDC\nADBC\nBDCA\nDCBA\nBACD\nBADC\nBDAC\nCBDA\nDBCA\nDCAB'
);</lang>
 
{{Out}}
 
<lang JavaScript>["DBAC"]</lang>
 
=={{header|jq}}==
9,659

edits