Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
23
Task/Permutations/JavaScript/permutations-1.js
Normal file
23
Task/Permutations/JavaScript/permutations-1.js
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
<html><head><title>Permutations</title></head>
|
||||
<body><pre id="result"></pre>
|
||||
<script type="text/javascript">
|
||||
var d = document.getElementById('result');
|
||||
|
||||
function perm(list, ret)
|
||||
{
|
||||
if (list.length == 0) {
|
||||
var row = document.createTextNode(ret.join(' ') + '\n');
|
||||
d.appendChild(row);
|
||||
return;
|
||||
}
|
||||
for (var i = 0; i < list.length; i++) {
|
||||
var x = list.splice(i, 1);
|
||||
ret.push(x);
|
||||
perm(list, ret);
|
||||
ret.pop();
|
||||
list.splice(i, 0, x);
|
||||
}
|
||||
}
|
||||
|
||||
perm([1, 2, 'A', 4], []);
|
||||
</script></body></html>
|
||||
12
Task/Permutations/JavaScript/permutations-2.js
Normal file
12
Task/Permutations/JavaScript/permutations-2.js
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
function perm(a) {
|
||||
if (a.length < 2) return [a];
|
||||
var c, d, b = [];
|
||||
for (c = 0; c < a.length; c++) {
|
||||
var e = a.splice(c, 1),
|
||||
f = perm(a);
|
||||
for (d = 0; d < f.length; d++) b.push([e].concat(f[d]));
|
||||
a.splice(c, 0, e[0])
|
||||
} return b
|
||||
}
|
||||
|
||||
console.log(perm(['Aardvarks', 'eat', 'ants']).join("\n"));
|
||||
6
Task/Permutations/JavaScript/permutations-3.js
Normal file
6
Task/Permutations/JavaScript/permutations-3.js
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
Aardvarks,eat,ants
|
||||
Aardvarks,ants,eat
|
||||
eat,Aardvarks,ants
|
||||
eat,ants,Aardvarks
|
||||
ants,Aardvarks,eat
|
||||
ants,eat,Aardvarks
|
||||
35
Task/Permutations/JavaScript/permutations-4.js
Normal file
35
Task/Permutations/JavaScript/permutations-4.js
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
(function () {
|
||||
'use strict';
|
||||
|
||||
// permutations :: [a] -> [[a]]
|
||||
var permutations = function (xs) {
|
||||
return xs.length ? concatMap(function (x) {
|
||||
return concatMap(function (ys) {
|
||||
return [[x].concat(ys)];
|
||||
}, permutations(delete_(x, xs)));
|
||||
}, xs) : [[]];
|
||||
};
|
||||
|
||||
// GENERIC FUNCTIONS
|
||||
|
||||
// concatMap :: (a -> [b]) -> [a] -> [b]
|
||||
var concatMap = function (f, xs) {
|
||||
return [].concat.apply([], xs.map(f));
|
||||
};
|
||||
|
||||
// delete :: Eq a => a -> [a] -> [a]
|
||||
var delete_ = function (x, xs) {
|
||||
return deleteBy(function (a, b) {
|
||||
return a === b;
|
||||
}, x, xs);
|
||||
};
|
||||
|
||||
// deleteBy :: (a -> a -> Bool) -> a -> [a] -> [a]
|
||||
var deleteBy = function (f, x, xs) {
|
||||
return xs.length > 0 ? f(x, xs[0]) ? xs.slice(1) :
|
||||
[xs[0]].concat(deleteBy(f, x, xs.slice(1))) : [];
|
||||
};
|
||||
|
||||
// TEST
|
||||
return permutations(['Aardvarks', 'eat', 'ants']);
|
||||
})();
|
||||
3
Task/Permutations/JavaScript/permutations-5.js
Normal file
3
Task/Permutations/JavaScript/permutations-5.js
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
[["Aardvarks", "eat", "ants"], ["Aardvarks", "ants", "eat"],
|
||||
["eat", "Aardvarks", "ants"], ["eat", "ants", "Aardvarks"],
|
||||
["ants", "Aardvarks", "eat"], ["ants", "eat", "Aardvarks"]]
|
||||
39
Task/Permutations/JavaScript/permutations-6.js
Normal file
39
Task/Permutations/JavaScript/permutations-6.js
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
(() => {
|
||||
'use strict';
|
||||
|
||||
// permutations :: [a] -> [[a]]
|
||||
const permutations = xs => {
|
||||
const go = xs => xs.length ? (
|
||||
concatMap(
|
||||
x => concatMap(
|
||||
ys => [[x].concat(ys)],
|
||||
go(delete_(x, xs))), xs
|
||||
)
|
||||
) : [[]];
|
||||
return go(xs);
|
||||
};
|
||||
|
||||
// GENERIC FUNCTIONS ----------------------------------
|
||||
|
||||
// concatMap :: (a -> [b]) -> [a] -> [b]
|
||||
const concatMap = (f, xs) =>
|
||||
xs.reduce((a, x) => a.concat(f(x)), []);
|
||||
|
||||
|
||||
// delete :: Eq a => a -> [a] -> [a]
|
||||
const delete_ = (x, xs) => {
|
||||
const go = xs => {
|
||||
return 0 < xs.length ? (
|
||||
(x === xs[0]) ? (
|
||||
xs.slice(1)
|
||||
) : [xs[0]].concat(go(xs.slice(1)))
|
||||
) : [];
|
||||
}
|
||||
return go(xs);
|
||||
};
|
||||
|
||||
// TEST
|
||||
return JSON.stringify(
|
||||
permutations(['Aardvarks', 'eat', 'ants'])
|
||||
);
|
||||
})();
|
||||
3
Task/Permutations/JavaScript/permutations-7.js
Normal file
3
Task/Permutations/JavaScript/permutations-7.js
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
[["Aardvarks", "eat", "ants"], ["Aardvarks", "ants", "eat"],
|
||||
["eat", "Aardvarks", "ants"], ["eat", "ants", "Aardvarks"],
|
||||
["ants", "Aardvarks", "eat"], ["ants", "eat", "Aardvarks"]]
|
||||
42
Task/Permutations/JavaScript/permutations-8.js
Normal file
42
Task/Permutations/JavaScript/permutations-8.js
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
(() => {
|
||||
'use strict';
|
||||
|
||||
// permutations :: [a] -> [[a]]
|
||||
const permutations = xs =>
|
||||
xs.reduceRight(
|
||||
(a, x) => concatMap(
|
||||
xs => enumFromTo(0, xs.length)
|
||||
.map(n => xs.slice(0, n)
|
||||
.concat(x)
|
||||
.concat(xs.slice(n))
|
||||
),
|
||||
a
|
||||
),
|
||||
[[]]
|
||||
);
|
||||
|
||||
// GENERIC FUNCTIONS ----------------------------------
|
||||
|
||||
// concatMap :: (a -> [b]) -> [a] -> [b]
|
||||
const concatMap = (f, xs) =>
|
||||
xs.reduce((a, x) => a.concat(f(x)), []);
|
||||
|
||||
// ft :: Int -> Int -> [Int]
|
||||
const enumFromTo = (m, n) =>
|
||||
Array.from({
|
||||
length: 1 + n - m
|
||||
}, (_, i) => m + i);
|
||||
|
||||
// showLog :: a -> IO ()
|
||||
const showLog = (...args) =>
|
||||
console.log(
|
||||
args
|
||||
.map(JSON.stringify)
|
||||
.join(' -> ')
|
||||
);
|
||||
|
||||
// TEST -----------------------------------------------
|
||||
showLog(
|
||||
permutations([1, 2, 3])
|
||||
);
|
||||
})();
|
||||
Loading…
Add table
Add a link
Reference in a new issue