Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
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