How to resolve the algorithm General FizzBuzz step by step in the AppleScript programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm General FizzBuzz step by step in the AppleScript programming language

Table of Contents

Problem Statement

Write a generalized version of FizzBuzz that works for any list of factors, along with their words. This is basically a "fizzbuzz" implementation where the user supplies the parameters. The user will enter the max number, then they will enter the factors to be calculated along with the corresponding word to be printed. For simplicity's sake, assume the user will input an integer as the max number and 3 factors, each with a word associated with them.

For example, given: In other words: For this example, print the numbers 1 through 20, replacing every multiple of 3 with "Fizz", every multiple of 5 with "Buzz", and every multiple of 7 with "Baxx". In the case where a number is a multiple of at least two factors, print each of the words associated with those factors in the order of least to greatest factor. For instance, the number 15 is a multiple of both 3 and 5; print "FizzBuzz". If the max number was 105 instead of 20, you would print "FizzBuzzBaxx" because it's a multiple of 3, 5, and 7.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm General FizzBuzz step by step in the AppleScript programming language

Source code in the applescript programming language

--------------------- GENERAL FIZZBUZZ -------------------

-- fizzEtc :: [(Int, String)] -> [Symbol]
on fizzEtc(rules)
    -- A non-finite sequence of fizzEtc symbols,
    -- as defined by the given list of rules.
    script numberOrNoise
        on |λ|(n)
            script ruleMatch
                on |λ|(a, mk)
                    set {m, k} to mk
                    
                    if 0 = (n mod m) then
                        if integer is class of a then
                            k
                        else
                            a & k
                        end if
                    else
                        a
                    end if
                end |λ|
            end script
            
            foldl(ruleMatch, n, rules)
        end |λ|
    end script
    
    fmapGen(numberOrNoise, enumFrom(1))
end fizzEtc


--------------------------- TEST -------------------------
on run
    
    unlines(take(20, ¬
        fizzEtc({{3, "Fizz"}, {5, "Buzz"}, {7, "Baxx"}})))
    
end run


------------------------- GENERIC ------------------------

-- enumFrom :: Enum a => a -> [a]
on enumFrom(x)
    script
        property v : missing value
        on |λ|()
            if missing value is not v then
                set v to 1 + v
            else
                set v to x
            end if
            return v
        end |λ|
    end script
end enumFrom


-- fmapGen <$> :: (a -> b) -> Gen [a] -> Gen [b]
on fmapGen(f, gen)
    script
        property g : mReturn(f)
        on |λ|()
            set v to gen's |λ|()
            if v is missing value then
                v
            else
                g's |λ|(v)
            end if
        end |λ|
    end script
end fmapGen


-- foldl :: (a -> b -> a) -> a -> [b] -> a
on foldl(f, startValue, xs)
    tell mReturn(f)
        set v to startValue
        set lng to length of xs
        repeat with i from 1 to lng
            set v to |λ|(v, item i of xs, i, xs)
        end repeat
        return v
    end tell
end foldl


-- mReturn :: First-class m => (a -> b) -> m (a -> b)
on mReturn(f)
    -- 2nd class handler function lifted into 1st class script wrapper. 
    if script is class of f then
        f
    else
        script
            property |λ| : f
        end script
    end if
end mReturn


-- take :: Int -> [a] -> [a]
-- take :: Int -> String -> String
on take(n, xs)
    set ys to {}
    repeat with i from 1 to n
        set v to |λ|() of xs
        if missing value is v then
            return ys
        else
            set end of ys to v
        end if
    end repeat
    return ys
end take


-- unlines :: [String] -> String
on unlines(xs)
    -- A single string formed by the intercalation
    -- of a list of strings with the newline character.
    set {dlm, my text item delimiters} to ¬
        {my text item delimiters, linefeed}
    set s to xs as text
    set my text item delimiters to dlm
    s
end unlines


  

You may also check:How to resolve the algorithm Smith numbers step by step in the Nim programming language
You may also check:How to resolve the algorithm Numbers with equal rises and falls step by step in the Fortran programming language
You may also check:How to resolve the algorithm Number names step by step in the Go programming language
You may also check:How to resolve the algorithm Named parameters step by step in the Maple programming language
You may also check:How to resolve the algorithm Partition function P step by step in the REXX programming language