4
1
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.
3Can we get more test cases? – Okx – 2017-03-20T11:35:48.147
2So.. It's like the snail climbing to escape the well, reads the number each time it moves? – Matthew Roh – 2017-03-20T11:38:13.277
5Consider allowing more flexible output, such as each subarray on a line without the blank lines, or an array of subarrays – Luis Mendo – 2017-03-20T11:48:06.917
Can I take input as
[array],F,B
? – HyperNeutrino – 2017-03-20T12:17:51.773Are we guaranteed that
F < len(array)
? – HyperNeutrino – 2017-03-20T12:21:31.293@HyperNeutrino Yes, input can be [array],F,B. – Roman Bruhanov – 2017-03-20T12:23:33.633
@HyperNeutrino There is no guarantee of F. in case of F >= len(array), If there is no endless or backward-movement break conditions, program should output one iteration (which will be whole array) and exit. "Run till the end of array" – Roman Bruhanov – 2017-03-20T12:27:12.887
Ok, i think it will be more interesting to print array once, if F if >= len(array). Even if F and B form bad conditions. – Roman Bruhanov – 2017-03-20T12:39:26.673
Nice first post here! For future challenges, I would recommend posting them in the Sandbox first so you can get suggestions and improve your challenge before posting!
– HyperNeutrino – 2017-03-20T12:57:01.570@RomanBruhanov Request to print array once if F >= len(array) would just add F=min(F,len(array)) to every code. Nothing very interesting – Dead Possum – 2017-03-20T14:19:22.113
Can we assume the entries in the array are all distinct? – Not a tree – 2017-03-21T00:06:18.507
Is it ok to print trailing newlines? – fergusq – 2017-03-22T09:03:01.870
"Be positive while dancing" Nice play on words! – Matthew Roh – 2017-03-22T10:18:44.580