How to resolve the algorithm FizzBuzz step by step in the APL programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm FizzBuzz step by step in the APL programming language

Table of Contents

Problem Statement

Write a program that prints the integers from   1   to   100   (inclusive).

But:

The   FizzBuzz   problem was presented as the lowest level of comprehension required to illustrate adequacy.

Let's start with the solution:

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

Source code in the apl programming language

⎕IO0
(L,'Fizz' 'Buzz' 'FizzBuzz')[¯1+(L×W=0)+W(100×~0=W)+W+/1 2×0=3 5|L1+100]


A[I]1+I(0A)/A('FIZZBUZZ' 'FIZZ’ 'BUZZ' 0)[2¨×(3 5)|¨1+100]


{  'Fizz' 'Buzz' 'FizzBuzz'[ +/1 2×0=3 5|] }¨1+100


{(‘FizzBuzz’ ‘Fizz’ ‘Buzz’,)[(0=15 3 5|)1]}¨100


     sv  fizzbuzz n; t;d
[1]   ⍝⍝ Solve the popular 'fizzbuzz' problem in APL.
[2]   ⍝⍝ \param n - highest number to compute (≥0)
[3]   ⍝⍝ \returns sv - a vector of strings representing the fizzbuzz solution for ⍳n
[4]   ⍝⍝     (note we return a string vector to avoid a mixed-type result; remove the
[5]   ⍝⍝     ⍕ function from the (⍕t[⍵]) term to see the difference).
[6]   ⍝⍝⍝⍝
[7]   tn   ⍝ the sequence 1..n itself which we'll pick from
[8]   ⍝  ... or the words 'fizz', 'buzz', 'fizzbuzz' depending on
[9]   ⍝  ... divisibility by 3 and/or 5
[10]  ⍝⎕←t   ⍝ (Uncomment to see during call)
[11] 
[12]  d1+(+  {((0=3|)) (2×(0=5|))} n)
[13]  ⍝ || || | |                     | ↓↓
[14]  ⍝ || || | |                     | ⍳n: generate range (1..n)
[15]  ⍝ || || | ↓.....................↓                 ↓↓
[16]  ⍝ || || | A dfn (lambda) taking its right arg (⍵, ⍳n here) to compute two boolean
[17]  ⍝ || || |   vectors(v12): divisibility by 3 and 5, respectively, for each of ⍳n
[18]  ⍝ || || ↓
[19]  ⍝ || || ⊃: Disclose ('lift-up' and pad w/zeros) the 'ragged' matrix of vectors (v12)
[20]  ⍝ || ||    holding divisibility by 3 and 5 of each ⍳n
[21]  ⍝ || ↓↓
[22]  ⍝ || +⌿: Sum (v12) row-wise to count divisibility (0=neither 3 nor 5, 1=3, 2=3 and 5)
[23]  ⍝ ↓↓
[24]  ⍝ 1+: Add one to (v12) to make them 1-based for indexing below:
[25]  ⍝⎕←d
[26] 
[27]  sv  { ((t[]) 'Fizz' 'Buzz' 'FizzBuzz') [d[]]}¨ n
[28]  ⍝    | |                                | |    | |
[29]  ⍝    | |                                | ↓....↓ |
[30]  ⍝    | |................................↓  idx   |
[31]  ⍝    | (      lookup output vector      )        |
[32]  ⍝    ↓...........................................↓
[33]  ⍝    A dfn (lambda) taking as its right arg (⍵) ⍳n and using the 'each' (¨)
[34]  ⍝      operator to apply the lambda to each (idx) of ⍳n.
[35] 
[36]  ⍝⍝ USAGE
[37]  ⍝⍝ ⎕ ← ,fizzbuzz 15
[38]  ⍝ 1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz
    


  

You may also check:How to resolve the algorithm Repeat a string step by step in the Transact-SQL programming language
You may also check:How to resolve the algorithm Run-length encoding step by step in the Raku programming language
You may also check:How to resolve the algorithm Anagrams/Deranged anagrams step by step in the Run BASIC programming language
You may also check:How to resolve the algorithm Hello world/Standard error step by step in the Genie programming language
You may also check:How to resolve the algorithm Monte Carlo methods step by step in the Phix programming language