Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
18
Task/Combinations/JavaScript/combinations-1.js
Normal file
18
Task/Combinations/JavaScript/combinations-1.js
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
function bitprint(u) {
|
||||
var s="";
|
||||
for (var n=0; u; ++n, u>>=1)
|
||||
if (u&1) s+=n+" ";
|
||||
return s;
|
||||
}
|
||||
function bitcount(u) {
|
||||
for (var n=0; u; ++n, u=u&(u-1));
|
||||
return n;
|
||||
}
|
||||
function comb(c,n) {
|
||||
var s=[];
|
||||
for (var u=0; u<1<<n; u++)
|
||||
if (bitcount(u)==c)
|
||||
s.push(bitprint(u))
|
||||
return s.sort();
|
||||
}
|
||||
comb(3,5)
|
||||
25
Task/Combinations/JavaScript/combinations-2.js
Normal file
25
Task/Combinations/JavaScript/combinations-2.js
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
function combinations(arr, k){
|
||||
var i,
|
||||
subI,
|
||||
ret = [],
|
||||
sub,
|
||||
next;
|
||||
for(i = 0; i < arr.length; i++){
|
||||
if(k === 1){
|
||||
ret.push( [ arr[i] ] );
|
||||
}else{
|
||||
sub = combinations(arr.slice(i+1, arr.length), k-1);
|
||||
for(subI = 0; subI < sub.length; subI++ ){
|
||||
next = sub[subI];
|
||||
next.unshift(arr[i]);
|
||||
ret.push( next );
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
combinations([0,1,2,3,4], 3);
|
||||
// produces: [[0, 1, 2], [0, 1, 3], [0, 1, 4], [0, 2, 3], [0, 2, 4], [0, 3, 4], [1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]]
|
||||
|
||||
combinations(["Crosby", "Stills", "Nash", "Young"], 3);
|
||||
// produces: [["Crosby", "Stills", "Nash"], ["Crosby", "Stills", "Young"], ["Crosby", "Nash", "Young"], ["Stills", "Nash", "Young"]]
|
||||
29
Task/Combinations/JavaScript/combinations-3.js
Normal file
29
Task/Combinations/JavaScript/combinations-3.js
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
(function () {
|
||||
|
||||
function comb(n, lst) {
|
||||
if (!n) return [[]];
|
||||
if (!lst.length) return [];
|
||||
|
||||
var x = lst[0],
|
||||
xs = lst.slice(1);
|
||||
|
||||
return comb(n - 1, xs).map(function (t) {
|
||||
return [x].concat(t);
|
||||
}).concat(comb(n, xs));
|
||||
}
|
||||
|
||||
|
||||
// [m..n]
|
||||
function range(m, n) {
|
||||
return Array.apply(null, Array(n - m + 1)).map(function (x, i) {
|
||||
return m + i;
|
||||
});
|
||||
}
|
||||
|
||||
return comb(3, range(0, 4))
|
||||
|
||||
.map(function (x) {
|
||||
return x.join(' ');
|
||||
}).join('\n');
|
||||
|
||||
})();
|
||||
46
Task/Combinations/JavaScript/combinations-4.js
Normal file
46
Task/Combinations/JavaScript/combinations-4.js
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
(function (n) {
|
||||
|
||||
// n -> [a] -> [[a]]
|
||||
function comb(n, lst) {
|
||||
if (!n) return [[]];
|
||||
if (!lst.length) return [];
|
||||
|
||||
var x = lst[0],
|
||||
xs = lst.slice(1);
|
||||
|
||||
return comb(n - 1, xs).map(function (t) {
|
||||
return [x].concat(t);
|
||||
}).concat(comb(n, xs));
|
||||
}
|
||||
|
||||
// f -> f
|
||||
function memoized(fn) {
|
||||
m = {};
|
||||
return function (x) {
|
||||
var args = [].slice.call(arguments),
|
||||
strKey = args.join('-');
|
||||
|
||||
v = m[strKey];
|
||||
if ('u' === (typeof v)[0])
|
||||
m[strKey] = v = fn.apply(null, args);
|
||||
return v;
|
||||
}
|
||||
}
|
||||
|
||||
// [m..n]
|
||||
function range(m, n) {
|
||||
return Array.apply(null, Array(n - m + 1)).map(function (x, i) {
|
||||
return m + i;
|
||||
});
|
||||
}
|
||||
|
||||
var fnMemoized = memoized(comb),
|
||||
lstRange = range(0, 4);
|
||||
|
||||
return fnMemoized(n, lstRange)
|
||||
|
||||
.map(function (x) {
|
||||
return x.join(' ');
|
||||
}).join('\n');
|
||||
|
||||
})(3);
|
||||
10
Task/Combinations/JavaScript/combinations-5.js
Normal file
10
Task/Combinations/JavaScript/combinations-5.js
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
0 1 2
|
||||
0 1 3
|
||||
0 1 4
|
||||
0 2 3
|
||||
0 2 4
|
||||
0 3 4
|
||||
1 2 3
|
||||
1 2 4
|
||||
1 3 4
|
||||
2 3 4
|
||||
61
Task/Combinations/JavaScript/combinations-6.js
Normal file
61
Task/Combinations/JavaScript/combinations-6.js
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
(() => {
|
||||
'use strict';
|
||||
|
||||
// ------------------ COMBINATIONS -------------------
|
||||
|
||||
// combinations :: Int -> [a] -> [[a]]
|
||||
const combinations = n =>
|
||||
xs => {
|
||||
const comb = n => xs => {
|
||||
return 1 > n ? [
|
||||
[]
|
||||
] : 0 === xs.length ? (
|
||||
[]
|
||||
) : (() => {
|
||||
const
|
||||
h = xs[0],
|
||||
tail = xs.slice(1);
|
||||
return comb(n - 1)(tail)
|
||||
.map(cons(h))
|
||||
.concat(comb(n)(tail));
|
||||
})()
|
||||
};
|
||||
return comb(n)(xs);
|
||||
};
|
||||
|
||||
// ---------------------- TEST -----------------------
|
||||
const main = () =>
|
||||
show(
|
||||
combinations(3)(
|
||||
enumFromTo(0)(4)
|
||||
)
|
||||
);
|
||||
|
||||
|
||||
// ---------------- GENERIC FUNCTIONS ----------------
|
||||
|
||||
// cons :: a -> [a] -> [a]
|
||||
const cons = x =>
|
||||
// A list constructed from the item x,
|
||||
// followed by the existing list xs.
|
||||
xs => [x].concat(xs);
|
||||
|
||||
|
||||
// enumFromTo :: Int -> Int -> [Int]
|
||||
const enumFromTo = m =>
|
||||
n => !isNaN(m) ? (
|
||||
Array.from({
|
||||
length: 1 + n - m
|
||||
}, (_, i) => m + i)
|
||||
) : enumFromTo_(m)(n);
|
||||
|
||||
|
||||
// show :: a -> String
|
||||
const show = (...x) =>
|
||||
JSON.stringify.apply(
|
||||
null, x.length > 1 ? [x[0], null, x[1]] : x
|
||||
);
|
||||
|
||||
// MAIN ---
|
||||
return main();
|
||||
})();
|
||||
91
Task/Combinations/JavaScript/combinations-7.js
Normal file
91
Task/Combinations/JavaScript/combinations-7.js
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
(() => {
|
||||
'use strict';
|
||||
|
||||
// ------------------ COMBINATIONS -------------------
|
||||
|
||||
// comb :: Int -> Int -> [[Int]]
|
||||
const comb = m =>
|
||||
n => combinations(m)(
|
||||
enumFromTo(0)(n - 1)
|
||||
);
|
||||
|
||||
// combinations :: Int -> [a] -> [[a]]
|
||||
const combinations = k =>
|
||||
xs => sort(
|
||||
filter(xs => k === xs.length)(
|
||||
subsequences(xs)
|
||||
)
|
||||
);
|
||||
|
||||
// --------------------- TEST ---------------------
|
||||
const main = () =>
|
||||
show(
|
||||
comb(3)(5)
|
||||
);
|
||||
|
||||
// ---------------- GENERIC FUNCTIONS ----------------
|
||||
|
||||
// cons :: a -> [a] -> [a]
|
||||
const cons = x =>
|
||||
// A list constructed from the item x,
|
||||
// followed by the existing list xs.
|
||||
xs => [x].concat(xs);
|
||||
|
||||
|
||||
// enumFromTo :: Int -> Int -> [Int]
|
||||
const enumFromTo = m =>
|
||||
n => !isNaN(m) ? (
|
||||
Array.from({
|
||||
length: 1 + n - m
|
||||
}, (_, i) => m + i)
|
||||
) : enumFromTo_(m)(n);
|
||||
|
||||
|
||||
// filter :: (a -> Bool) -> [a] -> [a]
|
||||
const filter = p =>
|
||||
// The elements of xs which match
|
||||
// the predicate p.
|
||||
xs => [...xs].filter(p);
|
||||
|
||||
|
||||
// list :: StringOrArrayLike b => b -> [a]
|
||||
const list = xs =>
|
||||
// xs itself, if it is an Array,
|
||||
// or an Array derived from xs.
|
||||
Array.isArray(xs) ? (
|
||||
xs
|
||||
) : Array.from(xs || []);
|
||||
|
||||
|
||||
// show :: a -> String
|
||||
const show = x =>
|
||||
// JSON stringification of a JS value.
|
||||
JSON.stringify(x)
|
||||
|
||||
|
||||
// sort :: Ord a => [a] -> [a]
|
||||
const sort = xs => list(xs).slice()
|
||||
.sort((a, b) => a < b ? -1 : (a > b ? 1 : 0));
|
||||
|
||||
|
||||
// subsequences :: [a] -> [[a]]
|
||||
// subsequences :: String -> [String]
|
||||
const subsequences = xs => {
|
||||
const
|
||||
// nonEmptySubsequences :: [a] -> [[a]]
|
||||
nonEmptySubsequences = xxs => {
|
||||
if (xxs.length < 1) return [];
|
||||
const [x, xs] = [xxs[0], xxs.slice(1)];
|
||||
const f = (r, ys) => cons(ys)(cons(cons(x)(ys))(r));
|
||||
return cons([x])(nonEmptySubsequences(xs)
|
||||
.reduceRight(f, []));
|
||||
};
|
||||
return ('string' === typeof xs) ? (
|
||||
cons('')(nonEmptySubsequences(xs.split(''))
|
||||
.map(x => ''.concat.apply('', x)))
|
||||
) : cons([])(nonEmptySubsequences(xs));
|
||||
};
|
||||
|
||||
// MAIN ---
|
||||
return main();
|
||||
})();
|
||||
7
Task/Combinations/JavaScript/combinations-8.js
Normal file
7
Task/Combinations/JavaScript/combinations-8.js
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
function combinations(k, arr, prefix = []) {
|
||||
if (prefix.length == 0) arr = [...Array(arr).keys()];
|
||||
if (k == 0) return [prefix];
|
||||
return arr.flatMap((v, i) =>
|
||||
combinations(k - 1, arr.slice(i + 1), [...prefix, v])
|
||||
);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue