Is it possible to loop with a variable jump?

Asked

Viewed 102 times

1

I wonder if it is possible to create a loop with variable jump. For example, in the cycles shoot the jump would be 2 and in even cycles the cycles would be 4.

I tried to do it with Python’s ternary operator a if True else b, but it doesn’t work because the range function doesn’t change during the loop.

for j in range(i, len(message), small if odd else big):
    print(j)
  • You added the tag encriptação on the question, what are you trying to do has to do with cryptography? In what sense? If possible, please contextualize more.

  • Rail Fence is gonna need a little more than a loop. Maybe 2 interlaced loops is better, the outside controlling the offset of the steps from the inside. The more on the "edge", the higher the offset, which normalizes in the middle.

  • Hehe I’m detecting a "XY problem"... :Q This is my first contact with Rail Fence, but at first glance I think there are better ways to implement it. Can I make a suggestion? Create a function or formula to choose the right line given the letter in the message (i.e. that zigzags between the lines). After that, for each letter you put it in the right line, first increasing it with dots until it is the right size. Then concatenate the lines and eliminate the dots.

2 answers

3

You can toggle the step when using the variable:

for j in range(1, 20, 3):
    print(j if j & 1 else j + 1)

Exit:

1
5
7
11
13
17
19

See working on IDEONE.


If you want to adjust the moment the steps occur, you can change the j + 1 for j - 1, or change the + 1 if side, or even change the initial offset, goes from the concrete case.

  • +1 because it is on the right track, but note that the order of jumps will be different if the initial value is even or odd.

  • @mgibsonbr really lacks a little more information in the question about the real case, there goes to adjust; I left a comment in this direction at the end. If the OP gives more details, I edit the answer. However, the observation is valid.

3

The response of Bacchus seems to me to be the only correct way, although some details escape me. Its sequence is:

         {  i,          se n == 0;
seq(n) = {  seq(n-1)+2, se n % 2 == 1;
         {  seq(n-1)+4, se n % 2 == 0.

That is to say, i, i+2, i+6, i+8, i+12, ... This sequence can be simplified to:

i + 0*3 - 0,
i + 1*3 - 1,
i + 2*3 - 0,
i + 3*3 - 1,
i + 4*3 - 0,
...

Where each term, then, is equal to i + n*3 - n%2. i.e. "go 3 by 3, subtracting 1 in odd terms". One way to implement this would be:

for j in [i + n*3 - n%2 for n in range(0, limite_superior)]:
    print(j)

Where limite_superior is the n, such that i + n*3 - n%2 >= len(message), i and..:

n*3 - n%2 >= len(message) - i
n*3 >= len(message) - i + n%2
n >= (len(message) - i + n%2)/3

But the Bacco solution is simpler and more elegant - just pay attention to the difference between the is even and odd (in one case the first deviation will be 2, the next will be 4).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.