Dancing arrays. Output them with some forward-backward moves

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.

Roman Bruhanov

Posted 2017-03-20T11:18:37.163

Reputation: 49

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.773

Are 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

Answers

1

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.

HyperNeutrino

Posted 2017-03-20T11:18:37.163

Reputation: 26 575

You can save bytes by removing the unnecessary indentation – Mr. Xcoder – 2017-03-20T13:14:38.293

1@Mr.Xcoder all the indentation in his code is necessary. – fəˈnɛtɪk – 2017-03-20T13:16:45.107

Oh yes sorry :|||. My bad... – Mr. Xcoder – 2017-03-20T13:17:03.143

1how would you use {} in the input? that would be unordered – Felipe Nardi Batista – 2017-03-20T13:18:44.680

@FelipeNardiBatista Whoops. I forgot about that. – HyperNeutrino – 2017-03-20T13:21:23.407

1

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]

fəˈnɛtɪk

Posted 2017-03-20T11:18:37.163

Reputation: 4 166

You could save one byte by moving o+=...; inside the for loop. – Luke – 2017-03-20T20:59:40.513

0

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]+[''])

Dead Possum

Posted 2017-03-20T11:18:37.163

Reputation: 3 256

"If endless loop or backward progression requested, program should print nothing and exit" You are printing the array once on endless loop... – fəˈnɛtɪk – 2017-03-20T14:04:29.117

@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? – Dead Possum – 2017-03-20T14:13:44.547

@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 – Dead Possum – 2017-03-20T14:38:19.273

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. – HyperNeutrino – 2017-03-20T14:48:14.337

@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 – Dead Possum – 2017-03-20T15:05:12.377

It still behaves as expected either way... I'm running it with s*m<l-F+1 and it still works properly. – HyperNeutrino – 2017-03-20T15:52:51.703

@HyperNeutrino Nope, it does not, check proof

– Dead Possum – 2017-03-20T15:56:53.957

Oh never mind, I didn't notice the missing end part. Whoops. – HyperNeutrino – 2017-03-20T16:01:10.587

0

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.

Magic Octopus Urn

Posted 2017-03-20T11:18:37.163

Reputation: 19 422

0

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.

Not a tree

Posted 2017-03-20T11:18:37.163

Reputation: 3 106

0

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.

G B

Posted 2017-03-20T11:18:37.163

Reputation: 11 099

0

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!

R. Kap

Posted 2017-03-20T11:18:37.163

Reputation: 4 730