views:

91

answers:

3

Hi,

I have written an algorithm which iteratively solves the problem. The first iteration consists of 6 steps and all the following iterations consist of 5 steps (first step is skipped).

What I want to calculate is the current (local) step in the iteration from current global step.

For example if there are 41 steps in total which means there are 8 iterations: indices from 1 to 6 belong to 1st iteration indices from 7 to 11 belong to second iteration ...

For calculating the current iteration I have written the following code:

if(currentStep <= 6)
        iteration = 1;
    else
        iteration = floor((currentStep - 7)/5) + 2;
    end

The problem remains in calculating local steps. in first iteration the performed steps are: 1, 2, 3, 4, 5, 6 in all the following iterations the performing steps are 2, 3, 4, 5, 6

So what has to be done is to transform the array of global steps

[1 2 3 4 5 6 7 8 9 10 11 12 13 ... 41] 

into array of local steps

[1 2 3 4 5 6 2 3 4 5 6 2 3 ... 6].

I would appreciate if anyone could help in finding the solution to a given problem.

Thank you!

A: 

Check this :

if(currentStep <= 6)  
{localStep = currentStep;}  
else  
{localStep = currentStep - ((iteration - 1) * 5);}
Himadri
@Himadri: There's a button for code formatting. Also, there's a button with a question mark where you find all the explanations.
Jonas
@Jonas Oh..Yes thanks Jonas!:-)
Himadri
+2  A: 

Here is the solution in python:

L = range(1,42) # so L = [1,2,...,41]
s = [(i-2)%5+2 for i in L]
# adjust for the first step:
s[0]=1
# now s = [1,2,3,4,5,6,2,3,4,...,5,6]
Olivier
+3  A: 
local_step = [1 mod([0:39],5)+2]
James