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 i
s even and odd (in one case the first deviation will be 2
, the next will be 4
).
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.– mgibsonbr
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.
– Bacco
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.
– mgibsonbr