McNuggets problem: Difference between revisions

→‎{{header|JavaScript}}: Added a JS draft
(→‎{{header|Python}}: Added a functional Python variant.)
(→‎{{header|JavaScript}}: Added a JS draft)
Line 110:
Just (x, _) -> show x
Nothing -> "No unreachable quantities found ..."</lang>
<pre>43</pre>
 
=={{header|JavaScript}}==
<lang javascript>(() => {
'use strict';
 
// main :: IO ()
const main = () => {
 
const
upTo = enumFromTo(0),
nuggets = new Set(
bindList(
upTo(quot(100, 6)),
x => bindList(
upTo(quot(100, 9)),
y => bindList(
upTo(quot(100, 20)),
z => {
const v = sum([6 * x, 9 * y, 20 * z]);
return 101 > v ? (
[v]
) : [];
}
),
)
)
),
gaps = filter(
x => !nuggets.has(x),
enumFromThenTo(100, 99, 1)
);
 
return 0 < gaps.length ? (
gaps[0]
) : 'No unreachable quantities found in this range';
};
 
// GENERIC FUNCTIONS ----------------------------------
 
// bindList (>>=) :: [a] -> (a -> [b]) -> [b]
const bindList = (xs, mf) => [].concat.apply([], xs.map(mf));
 
 
// enumFromThenTo :: Int -> Int -> Int -> [Int]
const enumFromThenTo = (x1, x2, y) => {
const d = x2 - x1;
return Array.from({
length: Math.floor(y - x2) / d + 2
}, (_, i) => x1 + (d * i));
};
 
// ft :: Int -> Int -> [Int]
const enumFromTo = m => n =>
Array.from({
length: 1 + n - m
}, (_, i) => m + i);
 
// filter :: (a -> Bool) -> [a] -> [a]
const filter = (f, xs) => xs.filter(f);
 
// quot :: Int -> Int -> Int
const quot = (n, m) => Math.floor(n / m);
 
// sum :: [Num] -> Num
const sum = xs => xs.reduce((a, x) => a + x, 0);
 
// MAIN ---
return console.log(
main()
);
})();</lang>
{{Out}}
<pre>43</pre>
 
Line 120 ⟶ 193:
 
Technically, we could have used 100 in place of 101 when we were finding how many pure McNugget numbers were in each series (because 100 is obviously a McNugget number), but it's not like that's a problem, either.
 
=={{header|Kotlin}}==
{{trans|Go}}
9,659

edits