Should Repeat a Number of Times

Should repeat a number of times

In the below part, you passed only 1 argument instead 2.

describe "repeat" do
it "should repeat" do
repeat("hello").should == "hello hello"
# ^ why only 1 argument ?
end

As per your code, it should be repeat("hello", 2).should == "hello hello".

As per commented hints, you can write also :-

def repeat(x, y = 2)
y.times { print x + ‘ ‘ }
end

Now, the test code you wrote, will work without error, with above modified method definition.

require simon_says

describe "repeat" do
it "should repeat" do
# here you are not passsing the second argument. But *repeat* method has `2`
# as its default value of the second argument. So no issue will be here.
repeat("hello").should == "hello hello"
end

it "should repeat a number of times" do
repeat("hello", 3).should == "hello hello hello"
end
end

Read this default argument to know, how default argument works in Ruby.

How to repeat a while loop a certain number of times

As stated by @R2RT, you need to reset w after each r loop. Try writing this:

import random
a = []
w = 0
r = 0

while r < 3:
while w<4:
x = random.uniform(1,10)
print(x)
print(w)
a.append(w+x)
print(a)
w = w+1
r += 1
w = 0

How do you repeat a javascript function a set number of times, at a set interval?

I did vote to @JamesT answer, but here is aother version that I made based on his great answer:

no mutation - get as a param the number of repeats

const yourFunction = () => console.log('hi')

function runner(repeats = 1) {
if (repeats > 0) {
yourFunction()
setTimeout(() => runner(repeats - 1), 5000)
}
}

Repeat a string in JavaScript a number of times

These days, the repeat string method is implemented almost everywhere. (It is not in Internet Explorer.) So unless you need to support older browsers, you can simply write:

"a".repeat(10)

Before repeat, we used this hack:

Array(11).join("a") // create string with 10 a's: "Should Repeat a Number of Timesaa"

(Note that an array of length 11 gets you only 10 "a"s, since Array.join puts the argument between the array elements.)

Simon also points out that according to this benchmark, it appears that it's faster in Safari and Chrome (but not Firefox) to repeat a character multiple times by simply appending using a for loop (although a bit less concise).

Repeat a block of code a fixed number of times

Your attempts to optimize the loop by using some construct (incl. manually cutting & pasting the code) to optimize the loop's execution speed are ill-advised. Don't do it; it would probably "un-optimize" the execution speed instead.

In any C++ implementation I've ever encountered (MSVC 6.0, 2003, 2005, 2010, GCC various versions, Diab various versions), there is absolutely zero, sorry I didn't stress that enough, ZERO, time involved with allocating a loop counting variable, assuming any other variables were allocated for the function in which the loop counting variable is allocated. For a simple loop that makes no function calls, the loop counting variable may never even make it out to memory; it may be held entirely in a single CPU register for its entire lifetime. Even if it is stored in memory, it would be on the runtime stack, and space for it (and any other local variables) would be claimed all at once in a single operation, which takes no more or less time depending on the number of variables allocated on the stack. Local variables like your loop counter variable are allocated on the stack, and stack allocations are CHEAP CHEAP CHEAP, as opposed to heap allocations.

Example loop counter variable allocation on the stack:

for (int i=0; i<50; ++i) {
....
}

Another example loop counter variable allocation on the stack:

int i = 0;
for (; i<50; ++i) {
....
}

Example loop counter variable allocated on the heap (don't do this; it's stupid):

int* ip = new int;
for (*ip=0; *ip<50; ++(*ip)) {
....
}
delete ip;

Now to address the issue of attempting to optimize your loop by manually copying & pasting instead of using a loop & counter:

What you're considering doing is a manual form of loop unrolling. Loop unrolling is an optimization that compilers sometimes use for reducing the overhead involved in a loop. Compilers can do it only if the number of iterations of the loop can be known at compile time (i.e. the number of iterations is a constant, even if the constant involves computation based on other constants). In some cases, the compiler may determine that it is worthwhile to unroll the loop, but often it won't unroll it completely. For instance, in your example, the compiler may determine that it would be a speed advantage to unroll the loop from 50 iterations out to only 10 iterations with 5 copies of the loop body. The loop variable would still be there, but instead of doing 50 comparisons of the loop counter, now the code only has to do the comparison 10 times. It's a tradeoff, because the 5 copies of the loop body eat up 5 times as much space in the cache, which means that loading those extra copies of the same instructions forces the cache to evict (throw out) that many instructions that are already in the cache and which you might have wanted to stay in the cache. Also, loading those 4 extra copies of the loop body instructions from main memory takes much, much longer than simply grabbing the already-loaded instructions from the cache in the case where the loop isn't unrolled at all.

So all in all, it's often more advantageous to just use only one copy of the loop body and go ahead and leave the loop logic in place. (I.e. don't do any loop unrolling at all.)



Related Topics



Leave a reply



Submit