The task is to create a program (or a function), which outputs array elements in the order, determined by two numbers F and B. F is number of elements to print, B is the rollback distance (from last printed element). Each element must be printed on separate line and blank line must be inserted after each rollback. Run till the end of array. If endless loop or backward progression requested, program should print nothing and exit. Same for empty arrays.
F and B cannot be negative numbers. Be positive while dancing!
For example, if given:
array is 2, 3, 5, 7, 11, 101, 131;
F is 4;
B is 1;
then output must be:
2
3
5
7
5
7
11
101
11
101
131
Init array, F and B as you like.
Shortest code wins. Imperative beauty will be enjoyed.
Test cases:
Empty array.
Any F.
Any B.
Produces no output (array is empty).
Array: [42]
F = 0;
B = 1;
Produces no output (zero elements to print).
Array: [42, 17, 84]
F = 2;
B = 1;
Produces no output (endless printing).
Array: [7, 16, 65]
F = 2;
B = 3;
Produces no output (will be moving backward to negative indices).
Array: [7, 16, 65]
F = 2;
B = 0;
Output:
7
16
16
65
Array: [7, 16, 65]
F = 3;
B = 2;
Output:
7
16
65
(no rollback, end of array is reached).
Array: [7, 16, 65]
F = 17;
B = 15;
Output:
7
16
65
(run till the end)
Array: [7, 16, 65]
F = 17;
B = 16;
Output:
7
16
65
Whole array can be printed in first iteration, run till the end is working, even if F and B form endless-loop condition.
[array],F,B
? \$\endgroup\$F < len(array)
? \$\endgroup\$