Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
21
Task/Department-numbers/JavaScript/department-numbers-1.js
Normal file
21
Task/Department-numbers/JavaScript/department-numbers-1.js
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
(function () {
|
||||
'use strict';
|
||||
|
||||
// concatMap :: (a -> [b]) -> [a] -> [b]
|
||||
function concatMap(f, xs) {
|
||||
return [].concat.apply([], xs.map(f));
|
||||
};
|
||||
|
||||
return '(Police, Sanitation, Fire)\n' +
|
||||
concatMap(function (x) {
|
||||
return concatMap(function (y) {
|
||||
return concatMap(function (z) {
|
||||
return z !== y && 1 <= z && z <= 7 ? [
|
||||
[x, y, z]
|
||||
] : [];
|
||||
}, [12 - (x + y)]);
|
||||
}, [1, 2, 3, 4, 5, 6, 7]);
|
||||
}, [2, 4, 6])
|
||||
.map(JSON.stringify)
|
||||
.join('\n');
|
||||
})();
|
||||
79
Task/Department-numbers/JavaScript/department-numbers-2.js
Normal file
79
Task/Department-numbers/JavaScript/department-numbers-2.js
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
(function () {
|
||||
'use strict';
|
||||
|
||||
// NUMBERING CONSTRAINTS --------------------------------------------------
|
||||
|
||||
// options :: Int -> Int -> Int -> [(Int, Int, Int)]
|
||||
function options(lo, hi, total) {
|
||||
var bind = flip(concatMap),
|
||||
ds = enumFromTo(lo, hi);
|
||||
|
||||
return bind(filter(even, ds),
|
||||
function (x) { // X is even,
|
||||
return bind(filter(function (d) { return d !== x; }, ds),
|
||||
function (y) { // Y is distinct from X,
|
||||
return bind([total - (x + y)],
|
||||
function (z) { // Z sums with x and y to total, and is in ds.
|
||||
return z !== y && lo <= z && z <= hi ? [
|
||||
[x, y, z]
|
||||
] : [];
|
||||
})})})};
|
||||
|
||||
// GENERIC FUNCTIONS ------------------------------------------------------
|
||||
|
||||
// concatMap :: (a -> [b]) -> [a] -> [b]
|
||||
function concatMap(f, xs) {
|
||||
return [].concat.apply([], xs.map(f));
|
||||
};
|
||||
|
||||
// enumFromTo :: Int -> Int -> [Int]
|
||||
function enumFromTo(m, n) {
|
||||
return Array.from({
|
||||
length: Math.floor(n - m) + 1
|
||||
}, function (_, i) {
|
||||
return m + i;
|
||||
});
|
||||
};
|
||||
|
||||
// even :: Integral a => a -> Bool
|
||||
function even(n) {
|
||||
return n % 2 === 0;
|
||||
};
|
||||
|
||||
// filter :: (a -> Bool) -> [a] -> [a]
|
||||
function filter(f, xs) {
|
||||
return xs.filter(f);
|
||||
};
|
||||
|
||||
// flip :: (a -> b -> c) -> b -> a -> c
|
||||
function flip(f) {
|
||||
return function (a, b) {
|
||||
return f.apply(null, [b, a]);
|
||||
};
|
||||
};
|
||||
|
||||
// length :: [a] -> Int
|
||||
function length(xs) {
|
||||
return xs.length;
|
||||
};
|
||||
|
||||
// map :: (a -> b) -> [a] -> [b]
|
||||
function map(f, xs) {
|
||||
return xs.map(f);
|
||||
};
|
||||
|
||||
// show :: a -> String
|
||||
function show(x) {
|
||||
return JSON.stringify(x);
|
||||
}; //, null, 2);
|
||||
|
||||
// unlines :: [String] -> String
|
||||
function unlines(xs) {
|
||||
return xs.join('\n');
|
||||
};
|
||||
|
||||
// TEST -------------------------------------------------------------------
|
||||
var xs = options(1, 7, 12);
|
||||
return '(Police, Sanitation, Fire)\n\n' +
|
||||
unlines(map(show, xs)) + '\n\nNumber of options: ' + length(xs);
|
||||
})();
|
||||
22
Task/Department-numbers/JavaScript/department-numbers-3.js
Normal file
22
Task/Department-numbers/JavaScript/department-numbers-3.js
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
(() => {
|
||||
"use strict";
|
||||
|
||||
const
|
||||
label = "(Police, Sanitation, Fire)",
|
||||
solutions = [2, 4, 6]
|
||||
.flatMap(
|
||||
x => [1, 2, 3, 4, 5, 6, 7]
|
||||
.flatMap(
|
||||
y => [12 - (x + y)]
|
||||
.flatMap(
|
||||
z => z !== y && 1 <= z && z <= 7 ? [
|
||||
[x, y, z]
|
||||
] : []
|
||||
)
|
||||
)
|
||||
)
|
||||
.map(JSON.stringify)
|
||||
.join("\n");
|
||||
|
||||
return `${label}\n${solutions}`;
|
||||
})();
|
||||
51
Task/Department-numbers/JavaScript/department-numbers-4.js
Normal file
51
Task/Department-numbers/JavaScript/department-numbers-4.js
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
(() => {
|
||||
"use strict";
|
||||
|
||||
// -------------- NUMBERING CONSTRAINTS --------------
|
||||
|
||||
// options :: Int -> Int -> Int -> [(Int, Int, Int)]
|
||||
const options = lo => hi => total => {
|
||||
const
|
||||
bind = xs => f => xs.flatMap(f),
|
||||
ds = enumFromTo(lo)(hi);
|
||||
|
||||
return bind(ds.filter(even))(
|
||||
x => bind(ds.filter(d => d !== x))(
|
||||
y => bind([total - (x + y)])(
|
||||
z => (z !== y && lo <= z && z <= hi) ? [
|
||||
[x, y, z]
|
||||
] : []
|
||||
)
|
||||
)
|
||||
);
|
||||
};
|
||||
|
||||
// ---------------------- TEST -----------------------
|
||||
const main = () => {
|
||||
const
|
||||
label = "(Police, Sanitation, Fire)",
|
||||
solutions = options(1)(7)(12),
|
||||
n = solutions.length,
|
||||
list = solutions
|
||||
.map(JSON.stringify)
|
||||
.join("\n");
|
||||
|
||||
return (
|
||||
`${label}\n\n${list}\n\nNumber of options: ${n}`
|
||||
);
|
||||
};
|
||||
|
||||
// ---------------- GENERIC FUNCTIONS ----------------
|
||||
|
||||
// enumFromTo :: Int -> Int -> [Int]
|
||||
const enumFromTo = m =>
|
||||
n => Array.from({
|
||||
length: 1 + n - m
|
||||
}, (_, i) => m + i);
|
||||
|
||||
// even :: Integral a => a -> Bool
|
||||
const even = n => n % 2 === 0;
|
||||
|
||||
// MAIN ---
|
||||
return main();
|
||||
})();
|
||||
Loading…
Add table
Add a link
Reference in a new issue