How to resolve the algorithm Repeat a string step by step in the AppleScript programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Repeat a string step by step in the AppleScript programming language

Table of Contents

Problem Statement

Take a string and repeat it some number of times.
Example: repeat("ha", 5)   =>   "hahahahaha" If there is a simpler/more efficient way to repeat a single “character” (i.e. creating a string filled with a certain character), you might want to show that as well (i.e. repeat-char("*", 5) => "*****").

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Repeat a string step by step in the AppleScript programming language

Source code in the applescript programming language

set str to "ha"
set final_string to ""
repeat 5 times
    set final_string to final_string & str
end repeat


replicate(5000, "ha")

-- Repetition by 'Egyptian multiplication' - 
-- progressively doubling a list, appending
-- stages of doubling to an accumulator where needed for 
-- binary assembly of a target length.

-- replicate :: Int -> String -> String
on replicate(n, s)
    set out to ""
    if n < 1 then return out
    set dbl to s
    
    repeat while (n > 1)
        if (n mod 2) > 0 then set out to out & dbl
        set n to (n div 2)
        set dbl to (dbl & dbl)
    end repeat
    return out & dbl
end replicate


  

You may also check:How to resolve the algorithm Quine step by step in the Plain TeX programming language
You may also check:How to resolve the algorithm Memory allocation step by step in the 360 Assembly programming language
You may also check:How to resolve the algorithm Guess the number/With feedback (player) step by step in the Arturo programming language
You may also check:How to resolve the algorithm Return multiple values step by step in the Fōrmulæ programming language
You may also check:How to resolve the algorithm Exponentiation with infix operators in (or operating on) the base step by step in the Nim programming language