4
\$\begingroup\$

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.

\$\endgroup\$
13
  • 3
    \$\begingroup\$ Can we get more test cases? \$\endgroup\$
    – Okx
    Mar 20, 2017 at 11:35
  • 2
    \$\begingroup\$ So.. It's like the snail climbing to escape the well, reads the number each time it moves? \$\endgroup\$ Mar 20, 2017 at 11:38
  • 5
    \$\begingroup\$ Consider allowing more flexible output, such as each subarray on a line without the blank lines, or an array of subarrays \$\endgroup\$
    – Luis Mendo
    Mar 20, 2017 at 11:48
  • \$\begingroup\$ Can I take input as [array],F,B? \$\endgroup\$
    – hyper-neutrino
    Mar 20, 2017 at 12:17
  • \$\begingroup\$ Are we guaranteed that F < len(array)? \$\endgroup\$
    – hyper-neutrino
    Mar 20, 2017 at 12:21

7 Answers 7

1
\$\begingroup\$

JavaScript, 119 bytes

(a,b,c)=>{o="";for(s=++b;(s<(t=c.length)||s==b)&&s>=0&&b<a;)s-=b,o+=c.slice(s,s+=a<t?a:t).join`
`+`

`;return s>0?o:""}

Try it online!

Receives input in the format F, B, [Array]

\$\endgroup\$
1
  • \$\begingroup\$ You could save one byte by moving o+=...; inside the for loop. \$\endgroup\$
    – Luke
    Mar 20, 2017 at 20:59
1
\$\begingroup\$

Python 2, 135 128 126 bytes

Pretty naive solution:

a,f,b=input()
b+=1
l=len(a)
i=0
e=min(l,f)
while f>b:
    print'\n'.join(map(str,a)[i:e])+'\n';i=e-b
    if e==l:f=b
    e=min(l,e+f-b)

Try it online!

Input format is [array], F, B except you can also use (). (You can't use {} because that's unordered; thanks to Felipe Batista for pointing that out because I forgot).
Output format is as specified in the challenge, with a single trailing newline at the end of all output and no leading whitespace.

-7 bytes by changing if f>b: while 1: to while f>b
-2 bytes by changing break to f=b because that causes the while loop to exit.

\$\endgroup\$
5
  • \$\begingroup\$ You can save bytes by removing the unnecessary indentation \$\endgroup\$
    – Mr. Xcoder
    Mar 20, 2017 at 13:14
  • 1
    \$\begingroup\$ @Mr.Xcoder all the indentation in his code is necessary. \$\endgroup\$ Mar 20, 2017 at 13:16
  • \$\begingroup\$ Oh yes sorry :|||. My bad... \$\endgroup\$
    – Mr. Xcoder
    Mar 20, 2017 at 13:17
  • 1
    \$\begingroup\$ how would you use {} in the input? that would be unordered \$\endgroup\$ Mar 20, 2017 at 13:18
  • \$\begingroup\$ @FelipeNardiBatista Whoops. I forgot about that. \$\endgroup\$
    – hyper-neutrino
    Mar 20, 2017 at 13:21
0
\$\begingroup\$

05AB1E, 41 bytes

gD²³+‹iq}©0U[XD²<+Ÿv¹yè,}¶,³X+U®X³2*+-0‹#

Try it online!

That output format, and inability to limit inputs for F/B made this less fun, will explain soon.

\$\endgroup\$
0
\$\begingroup\$

Python 2, 116

Finally beat javascript!

Now in case of endless loop outputs full array only if it fits in one step, otherwise ouputs nothing. Output all array in every case of endless loop was harder.

Uncomment different test cases in footer in Try it online

a,F,B=input()
l=len(a)-F;m=F+~B or-1;s=l/m;s+=1+(s*m<l)
for i in range(s):print"\n".join(map(str,a)[m*i:F+i*m]+[''])

Explanation:

  a,F,B=input()
  l=len(a)-F            # length of input array minu F stored to compact code
  m=F+~B or-1           # get the difference beetween blocks of output
                        # if F-B-1 == 0, which means endless loop, makes m = -1, which will make s < 0 and for loop won't execute
  s=l/m                 # get number of blocks (does not count first block)
  s+=1+(s*m<l)          # +1 for first block (brackets are still needed)
                        # s*m<l-F equals +1 for numbers at the end of array, that are not enough to make full block
                        # s becomes 2 in case of F-B-1==0, but it doesn't matter
  for i in range(s):
                        # print elements from slices, joined by line separator
                        # additional empty element added at the end of each block to divide blocks with newline
    print"\n".join(map(str,a)[m*i:F+i*m]+[''])
\$\endgroup\$
8
  • \$\begingroup\$ "If endless loop or backward progression requested, program should print nothing and exit" You are printing the array once on endless loop... \$\endgroup\$ Mar 20, 2017 at 14:04
  • \$\begingroup\$ @fəˈnɛtɪk "Whole array can be printed in first iteration, run till the end is working, even if F and B form endless-loop condition." Doen't that counts? \$\endgroup\$ Mar 20, 2017 at 14:13
  • \$\begingroup\$ @fəˈnɛtɪk I re-read post again and brought a little fix. Indeed it seems that program should not output anything in case of endless loop, until whole array fits in one step \$\endgroup\$ Mar 20, 2017 at 14:38
  • \$\begingroup\$ You can save 2 bytes by changing 1+(s*m<1-F) to s*m<1-F+1 because that way you don't need the brackets. \$\endgroup\$
    – hyper-neutrino
    Mar 20, 2017 at 14:48
  • \$\begingroup\$ @HyperNeutrino No, I can't do that as (s*m<l-F) should evaluates to 0 or 1. If I remove brackets, than it would evaluates as (s*m<l)-F \$\endgroup\$ Mar 20, 2017 at 15:05
0
\$\begingroup\$

Ruby, 78 76 72 bytes

->a,f,b{i=b-=f-1;puts a[i,f]+[""]while 0>b&&a[f+i-=b];b<i&&puts(a[i,f])}

Explanation

  • Initialize the index so that the first step brings it back to zero.
  • Print f elements from the array starting with the current index, then a newline
  • Move the index f-b-1 elements forward, but only if (b-f+1) is negative
  • If the increment is not negative, or we reached the end of the array, stop.
  • If everything went Ok, the increment is negative, and the index is not (we took at least one step forward), in this case print the remaining elements.
\$\endgroup\$
0
\$\begingroup\$

Mathematica, 136 123 bytes

Here's my answer:

""<>r[#~(r=Riffle)~"\n"&/@Quiet@Check[Partition[#,#2,n=#2-#3-1,1,{}]~Take~-Floor[(L=#2-Length@#)/n-1],If[L<0,{},{#}]],"\n\n"]&

(where \ns are replaced by newlines). It's a function that takes a list of strings (e.g. {"1", "2", "3", ...}) and two integers.

Mathematica has a function that almost solves this challenge on its own, namely Partition[#, #2, #2-#3-1, 1, {}]. However, the output format isn't what we want, it returns errors if F and B would put the program into a loop, and it doesn't stop when it reaches the end of a list (so if the inputs are {"1", "2", "3"}, 3, 1, for instance, it returns {{"1", "2", "3"}, {"2", "3"}, {"3"}} instead of just {{"1", "2", "3"}}). We deal with those technicalities as follows:

  • ...~Take~-Floor[(L=#2-Length@#)/n-1] gets rid of the extra bits after we hit the end of the list.
  • Quiet@Check[...,If[L<0,{},{#}]] deals with the infinite loop cases by Checking if the earlier code gives errors (but it does it Quietly, so it doesn't actually print them), and instead returns an empty list or the entire list as appropriate.
  • ""<>r[#~(r=Riffle)~"\n"&/@...,"\n\n"]& formats the output properly, by putting newlines between everything, including double newlines between the separate groups, and string-joins that all together.
\$\endgroup\$
0
\$\begingroup\$

dc, 123 bytes

[[[dx]dx]sOzlF!>O]sO?1+sBsFlFlB-1>O_1sq[lq1+:Rlq1+sqz0<k]dskx0sd[[10PlqlB+sq]sWld0<W]sz[ldlF%0=zlq;Rplq1-dsqld1+sd0!>g]dsgx

Much longer than it should have been because of strict output regulations. Takes input on one line in the format

n_1 n_2 ... n_k F B

where n_i represents the ith element of the k length array. I hope this is an okay input format.

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.