How to resolve the algorithm McNuggets problem step by step in the JavaScript programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm McNuggets problem step by step in the JavaScript programming language

Table of Contents

Problem Statement

Calculate (from 0 up to a limit of 100) the largest non-McNuggets number (a number n which cannot be expressed with 6x + 9y + 20z = n where x, y and z are natural numbers).

Let's start with the solution:

Step by Step solution about How to resolve the algorithm McNuggets problem step by step in the JavaScript programming language

The provided JavaScript code defines a main function main that calculates and prints the smallest number greater than 100 that cannot be formed using a combination of 6, 9, and 20 cent coins.

Detailed Explanation:

Generic Functions:

  • dropWhile: Drops elements from the beginning of a list (xs) while the predicate function (p) holds true for each element.

  • enumFromThenTo: Generates an ascending range of integers from x1 to y with a step size of x2 - x1.

  • enumFromTo: Generates an ascending range of integers from m to n with a step size of 1.

  • quot: Calculates the quotient (integer division) of n by m.

  • sum: Computes the sum of a list of numbers (xs).

  • until: Executes a function f on a value x repeatedly until the predicate function p holds true for x.

Main Function (main):

  • Generates a list of potential nugget values:

    • size(n) generates a list of numbers from 0 to 99 that can be formed using coin denominations of n.
    • nuggets is a set of all possible nugget values that can be formed using these denominations.
  • Finds the smallest unreachable nugget:

    • xs is the list of potential nugget values greater than 100, excluding those already present in nuggets.
  • Returns the result:

    • If xs is non-empty, it returns the first element (smallest unreachable nugget). Otherwise, it returns a message indicating that no such nugget exists in the range.

Example Use:

The code is executed in the main function, and the result (the smallest unreachable nugget) is printed to the console. For example, running this code will output:

105

This indicates that the smallest number greater than 100 that cannot be formed using a combination of 6, 9, and 20 cent coins is 105.

Source code in the javascript programming language

(() => {
    'use strict';

    // main :: IO ()
    const main = () => {
        const
            size = n => enumFromTo(0)(
                quot(100, n)
            ),
            nuggets = new Set(
                size(6).flatMap(
                    x => size(9).flatMap(
                        y => size(20).flatMap(
                            z => {
                                const v = sum([6 * x, 9 * y, 20 * z]);
                                return 101 > v ? (
                                    [v]
                                ) : [];
                            }
                        ),
                    )
                )
            ),
            xs = dropWhile(
                x => nuggets.has(x),
                enumFromThenTo(100, 99, 1)
            );

        return 0 < xs.length ? (
            xs[0]
        ) : 'No unreachable quantities found in this range';
    };


    // GENERIC FUNCTIONS ----------------------------------

    // dropWhile :: (a -> Bool) -> [a] -> [a]
    const dropWhile = (p, xs) => {
        const lng = xs.length;
        return 0 < lng ? xs.slice(
            until(
                i => i === lng || !p(xs[i]),
                i => 1 + i,
                0
            )
        ) : [];
    };

    // 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);

    // 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);

    // until :: (a -> Bool) -> (a -> a) -> a -> a
    const until = (p, f, x) => {
        let v = x;
        while (!p(v)) v = f(v);
        return v;
    };

    // MAIN ---
    return console.log(
        main()
    );
})();


  

You may also check:How to resolve the algorithm Call a foreign-language function step by step in the Forth programming language
You may also check:How to resolve the algorithm Evolutionary algorithm step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Rot-13 step by step in the Zig programming language
You may also check:How to resolve the algorithm Variadic function step by step in the Ring programming language
You may also check:How to resolve the algorithm Range extraction step by step in the Oz programming language