How do I calculate the current index?

I wrote an algorithm that iteratively solves the problem. The first iteration consists of 6 steps, and all subsequent iterations consist of 5 steps (the first step is skipped).

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

For example, if there are 41 steps in total, that is, 8 iterations: indices 1 to 6 refer to the 1st iteration indices 7 to 11 refer to the second iteration ...

To calculate the current iteration, I wrote the following code:

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

      

The problem remains in the computation of local steps. at the first iteration, the stages are: 1, 2, 3, 4, 5, 6 in all subsequent iterations, steps 2, 3, 4, 5, 6 are performed

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

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

      

to an array of local steps

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

...

I would be grateful if someone could help find a solution to this problem.

Thanks!

+2


a source to share


3 answers


Here is a 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]

      

+2


a source


local_step = [1 mod([0:39],5)+2]

      



+3


a source


Check it:

if(currentStep <= 6)  
{localStep = currentStep;}  
else  
{localStep = currentStep - ((iteration - 1) * 5);}

      

0


a source







All Articles