How to resolve the algorithm Find the missing permutation step by step in the JavaScript programming language
How to resolve the algorithm Find the missing permutation step by step in the JavaScript programming language
Table of Contents
Problem Statement
Listed above are all-but-one of the permutations of the symbols A, B, C, and D, except for one permutation that's not listed.
Find that missing permutation.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Find the missing permutation step by step in the JavaScript programming language
The first code snippet is a javascript function that generates all possible permutations of a given string.
The second code snippet is a javascript function that takes a list of strings and returns the missing permutation.
The third code snippet is a javascript function that takes a list of strings and returns the missing permutation.
The fourth code snippet is a javascript function that takes a list of strings and returns the missing permutation.
The fifth code snippet is a javascript function that takes a list of strings and returns the missing permutation.
Source code in the javascript programming language
permute = function(v, m){ //v1.0
for(var p = -1, j, k, f, r, l = v.length, q = 1, i = l + 1; --i; q *= i);
for(x = [new Array(l), new Array(l), new Array(l), new Array(l)], j = q, k = l + 1, i = -1;
++i < l; x[2][i] = i, x[1][i] = x[0][i] = j /= --k);
for(r = new Array(q); ++p < q;)
for(r[p] = new Array(l), i = -1; ++i < l; !--x[1][i] && (x[1][i] = x[0][i],
x[2][i] = (x[2][i] + 1) % l), r[p][i] = m ? x[3][i] : v[x[3][i]])
for(x[3][i] = x[2][i], f = 0; !f; f = !f)
for(j = i; j; x[3][--j] == x[2][i] && (x[3][i] = x[2][i] = (x[2][i] + 1) % l, f = 1));
return r;
};
list = [ 'ABCD', 'CABD', 'ACDB', 'DACB', 'BCDA', 'ACBD', 'ADCB', 'CDAB',
'DABC', 'BCAD', 'CADB', 'CDBA', 'CBAD', 'ABDC', 'ADBC', 'BDCA',
'DCBA', 'BACD', 'BADC', 'BDAC', 'CBDA', 'DBCA', 'DCAB'];
all = permute(list[0].split('')).map(function(elem) {return elem.join('')});
missing = all.filter(function(elem) {return list.indexOf(elem) == -1});
print(missing); // ==> DBAC
(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] -> (a -> b) -> [b]
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
// (we can use fold/reduce, filter, or chain (concat map) here)
return chain(permutations('ABCD'.split('')), function (x) {
return lstSubSet.indexOf(x) === -1 ? [x] : [];
});
})(
'ABCD\nCABD\nACDB\nDACB\nBCDA\nACBD\nADCB\nCDAB\nDABC\nBCAD\nCADB\n\
CDBA\nCBAD\nABDC\nADBC\nBDCA\nDCBA\nBACD\nBADC\nBDAC\nCBDA\nDBCA\nDCAB'
);
["DBAC"]
(() => {
'use strict';
// transpose :: [[a]] -> [[a]]
let transpose = xs =>
xs[0].map((_, iCol) => xs
.map((row) => row[iCol]));
let xs = 'ABCD CABD ACDB DACB BCDA ACBD ADCB CDAB' +
' DABC BCAD CADB CDBA CBAD ABDC ADBC BDCA DCBA' +
' BACD BADC BDAC CBDA DBCA DCAB'
return transpose(xs.split(' ')
.map(x => x.split('')))
.map(col => col.reduce((a, x) => ( // count of each character in each column
a[x] = (a[x] || 0) + 1,
a
), {}))
.map(dct => { // character with frequency below mean of distribution ?
let ks = Object.keys(dct),
xs = ks.map(k => dct[k]),
mean = xs.reduce((a, b) => a + b, 0) / xs.length;
return ks.reduce(
(a, k) => a ? a : (dct[k] < mean ? k : undefined),
undefined
);
})
.join(''); // 4 chars as single string
// --> 'DBAC'
})();
(() => {
'use strict';
// MISSING PERMUTATION ---------------------------------------------------
// missingPermutation :: [String] -> String
const missingPermutation = xs =>
map(
// Rarest letter,
compose([
sort,
group,
curry(minimumBy)(comparing(length)),
head
]),
// in each column.
transpose(map(stringChars, xs))
)
.join('');
// GENERIC FUNCTIONAL PRIMITIVES -----------------------------------------
// transpose :: [[a]] -> [[a]]
const transpose = xs =>
xs[0].map((_, iCol) => xs.map(row => row[iCol]));
// sort :: Ord a => [a] -> [a]
const sort = xs => xs.sort();
// group :: Eq a => [a] -> [[a]]
const group = xs => groupBy((a, b) => a === b, xs);
// groupBy :: (a -> a -> Bool) -> [a] -> [[a]]
const groupBy = (f, xs) => {
const dct = xs.slice(1)
.reduce((a, x) => {
const
h = a.active.length > 0 ? a.active[0] : undefined,
blnGroup = h !== undefined && f(h, x);
return {
active: blnGroup ? a.active.concat(x) : [x],
sofar: blnGroup ? a.sofar : a.sofar.concat([a.active])
};
}, {
active: xs.length > 0 ? [xs[0]] : [],
sofar: []
});
return dct.sofar.concat(dct.active.length > 0 ? [dct.active] : []);
};
// length :: [a] -> Int
const length = xs => xs.length;
// comparing :: (a -> b) -> (a -> a -> Ordering)
const comparing = f =>
(x, y) => {
const
a = f(x),
b = f(y);
return a < b ? -1 : a > b ? 1 : 0
};
// minimumBy :: (a -> a -> Ordering) -> [a] -> a
const minimumBy = (f, xs) =>
xs.reduce((a, x) => a === undefined ? x : (
f(x, a) < 0 ? x : a
), undefined);
// head :: [a] -> a
const head = xs => xs.length ? xs[0] : undefined;
// map :: (a -> b) -> [a] -> [b]
const map = (f, xs) => xs.map(f)
// compose :: [(a -> a)] -> (a -> a)
const compose = fs => x => fs.reduce((a, f) => f(a), x);
// curry :: ((a, b) -> c) -> a -> b -> c
const curry = f => a => b => f(a, b);
// stringChars :: String -> [Char]
const stringChars = s => s.split('');
// TEST ------------------------------------------------------------------
return missingPermutation(["ABCD", "CABD", "ACDB", "DACB", "BCDA", "ACBD",
"ADCB", "CDAB", "DABC", "BCAD", "CADB", "CDBA", "CBAD", "ABDC", "ADBC",
"BDCA", "DCBA", "BACD", "BADC", "BDAC", "CBDA", "DBCA", "DCAB"
]);
// -> "DBAC"
})();
(() => {
'use strict';
// main :: IO ()
const main = () => {
const xs = [
'ABCD', 'CABD', 'ACDB', 'DACB', 'BCDA', 'ACBD',
'ADCB', 'CDAB', 'DABC', 'BCAD', 'CADB', 'CDBA',
'CBAD', 'ABDC', 'ADBC', 'BDCA', 'DCBA', 'BACD',
'BADC', 'BDAC', 'CBDA', 'DBCA', 'DCAB'
];
return xs.reduce(
(a, x) => zipWith(xor)(a)(codes(x)),
[0, 0, 0, 0]
).map(x => String.fromCodePoint(x)).join('')
};
// ---------------------- GENERIC ----------------------
// codes :: String -> [Int]
const codes = s =>
s.split('').map(c => c.codePointAt(0));
// xor :: Int -> Int -> Int
const xor = a =>
b => (a ^ b)
// zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
const zipWith = f =>
// A list constructed by zipping with a
// custom function, rather than with the
// default tuple constructor.
xs => ys => xs.slice(0).map(
(x, i) => f(x)(ys[i])
);
return main()
})();
You may also check:How to resolve the algorithm Flipping bits game step by step in the Raku programming language
You may also check:How to resolve the algorithm Amicable pairs step by step in the AppleScript programming language
You may also check:How to resolve the algorithm Straddling checkerboard step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Minkowski question-mark function step by step in the Python programming language
You may also check:How to resolve the algorithm Remove duplicate elements step by step in the Nanoquery programming language