How to resolve the algorithm Loops/Nested step by step in the AppleScript programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Loops/Nested step by step in the AppleScript programming language

Table of Contents

Problem Statement

Show a nested loop which searches a two-dimensional array filled with random numbers uniformly distributed over

[ 1 , … , 20 ]

{\displaystyle [1,\ldots ,20]}

. The loops iterate rows and columns of the array printing the elements until the value

20

{\displaystyle 20}

is met. Specifically, this task also shows how to break out of nested loops.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Loops/Nested step by step in the AppleScript programming language

Source code in the applescript programming language

on loopDemo(array, stopVal)
    set out to {}
    repeat with i from 1 to (count array)
        set inRow to item i of array
        set outRow to {}
        repeat with j from 1 to (count inRow)
            set n to item j of inRow
            set end of outRow to n
            if (n = stopVal) then exit repeat # <--
        end repeat
        set end of out to outRow
        if (n = stopVal) then exit repeat # <--
    end repeat
    
    return out
end loopDemo


on loopDemo(array, stopVal)
    set out to {}
    repeat with i from 1 to (count array)
        set inRow to item i of array
        set len to (count inRow)
        set n to beginning of inRow
        set outRow to {n}
        set j to 2
        repeat until ((j > len) or (n = stopVal)) # <--
            set n to item j of inRow
            set end of outRow to n
            set j to j + 1
        end repeat
        set end of out to outRow
        if (n = stopVal) then exit repeat # <--
    end repeat
    
    return out
end loopDemo


on loopDemo(array, stopVal)
    set out to {}
    repeat with i from 1 to (count array)
        set inRow to item i of array
        set outRow to {}
        repeat with j from 1 to (count inRow)
            set n to item j of inRow
            set end of outRow to n
            if (n = stopVal) then return out & {outRow} # <--
        end repeat
        set end of out to outRow
    end repeat
    
    return out
end loopDemo


local array, stopVal, row
set array to {}
set stopVal to 20
repeat 10 times
    set row to {}
    repeat 10 times
        set end of row to (random number from 1 to stopVal)
    end repeat
    set end of array to row
end repeat
loopDemo(array, stopVal) -- Any of the handlers above.


{{15, 8, 9, 8, 9, 9, 10, 16, 3, 6}, {11, 3, 14, 18, 17, 1, 16, 15, 14, 7}, {4, 20}}


  

You may also check:How to resolve the algorithm Sum of squares step by step in the IDL programming language
You may also check:How to resolve the algorithm Sorting algorithms/Gnome sort step by step in the Scala programming language
You may also check:How to resolve the algorithm Higher-order functions step by step in the Lingo programming language
You may also check:How to resolve the algorithm Bitmap/Bézier curves/Quadratic step by step in the MATLAB programming language
You may also check:How to resolve the algorithm Number reversal game step by step in the Crystal programming language