Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
|
|
@ -0,0 +1,24 @@
|
|||
<html><head><title>Donuts</title></head>
|
||||
<body><pre id='x'></pre><script type="application/javascript">
|
||||
function disp(x) {
|
||||
var e = document.createTextNode(x + '\n');
|
||||
document.getElementById('x').appendChild(e);
|
||||
}
|
||||
|
||||
function pick(n, got, pos, from, show) {
|
||||
var cnt = 0;
|
||||
if (got.length == n) {
|
||||
if (show) disp(got.join(' '));
|
||||
return 1;
|
||||
}
|
||||
for (var i = pos; i < from.length; i++) {
|
||||
got.push(from[i]);
|
||||
cnt += pick(n, got, i, from, show);
|
||||
got.pop();
|
||||
}
|
||||
return cnt;
|
||||
}
|
||||
|
||||
disp(pick(2, [], 0, ["iced", "jam", "plain"], true) + " combos");
|
||||
disp("pick 3 out of 10: " + pick(3, [], 0, "a123456789".split(''), false) + " combos");
|
||||
</script></body></html>
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
(function () {
|
||||
|
||||
// n -> [a] -> [[a]]
|
||||
function combsWithRep(n, lst) {
|
||||
return n ? (
|
||||
lst.length ? combsWithRep(n - 1, lst).map(function (t) {
|
||||
return [lst[0]].concat(t);
|
||||
}).concat(combsWithRep(n, lst.slice(1))) : []
|
||||
) : [[]];
|
||||
};
|
||||
|
||||
// If needed, we can derive a significantly faster version of
|
||||
// the simple recursive function above by memoizing it
|
||||
|
||||
// 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;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
return [
|
||||
|
||||
combsWithRep(2, ["iced", "jam", "plain"]),
|
||||
|
||||
// obtaining and applying a memoized version of the function
|
||||
memoized(combsWithRep)(3, range(1, 10)).length
|
||||
];
|
||||
|
||||
})();
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
[
|
||||
[["iced", "iced"], ["iced", "jam"], ["iced", "plain"],
|
||||
["jam", "jam"], ["jam", "plain"], ["plain", "plain"]],
|
||||
220
|
||||
]
|
||||
|
|
@ -0,0 +1,62 @@
|
|||
(() => {
|
||||
'use strict';
|
||||
|
||||
// COMBINATIONS WITH REPETITIONS -------------------------------------------
|
||||
|
||||
// combsWithRep :: Int -> [a] -> [[a]]
|
||||
const combsWithRep = (k, xs) => {
|
||||
const comb = (n, ys) => {
|
||||
if (0 === n) return ys;
|
||||
if (isNull(ys)) return comb(n - 1, map(pure, xs));
|
||||
|
||||
return comb(n - 1, concatMap(zs => {
|
||||
const h = head(zs);
|
||||
return map(x => [x].concat(zs), dropWhile(x => x !== h, xs));
|
||||
}, ys));
|
||||
};
|
||||
return comb(k, []);
|
||||
};
|
||||
|
||||
// GENERIC FUNCTIONS ------------------------------------------------------
|
||||
|
||||
// concatMap :: (a -> [b]) -> [a] -> [b]
|
||||
const concatMap = (f, xs) => [].concat.apply([], xs.map(f));
|
||||
|
||||
// dropWhile :: (a -> Bool) -> [a] -> [a]
|
||||
const dropWhile = (p, xs) => {
|
||||
let i = 0;
|
||||
for (let lng = xs.length;
|
||||
(i < lng) && p(xs[i]); i++) {}
|
||||
return xs.slice(i);
|
||||
};
|
||||
|
||||
// enumFromTo :: Int -> Int -> [Int]
|
||||
const enumFromTo = (m, n) =>
|
||||
Array.from({
|
||||
length: Math.floor(n - m) + 1
|
||||
}, (_, i) => m + i);
|
||||
|
||||
// head :: [a] -> Maybe a
|
||||
const head = xs => xs.length ? xs[0] : undefined;
|
||||
|
||||
// isNull :: [a] -> Bool
|
||||
const isNull = xs => (xs instanceof Array) ? xs.length < 1 : undefined;
|
||||
|
||||
// length :: [a] -> Int
|
||||
const length = xs => xs.length;
|
||||
|
||||
// map :: (a -> b) -> [a] -> [b]
|
||||
const map = (f, xs) => xs.map(f);
|
||||
|
||||
// pure :: a -> [a]
|
||||
const pure = x => [x];
|
||||
|
||||
// show :: a -> String
|
||||
const show = x => JSON.stringify(x, null, 2);
|
||||
|
||||
// TEST -------------------------------------------------------------------
|
||||
return show({
|
||||
twoFromThree: combsWithRep(2, ['iced', 'jam', 'plain']),
|
||||
threeFromTen: length(combsWithRep(3, enumFromTo(0, 9)))
|
||||
});
|
||||
})();
|
||||
Loading…
Add table
Add a link
Reference in a new issue