6
\$\begingroup\$

Challenge:

Input:

  • An integer n (> 0)
  • An integer-array a of size > 0 (which contains positive and negative values, but no zeroes)

Output:

  1. First output the first n items
  2. Then take the last item you've outputted (let's call it e), and go e steps forward if e is positive, or abs(e) steps backwards if e is negative. And from that position, output abs(e) items (again either going forward / backwards depending on positive / negative).
  3. Continue the process until you've either:
    • Outputted every distinct value of the array
    • Output one of the items at least n times

Example 1:

n = 3   
a = [2, 6, 9, 2, -4, -1, 3, 5, -7, 3, -2, 2, 2, 3]
     ^  ^  ^  ^   ^   ^  ^  ^   ^  ^   ^  ^  ^  ^
     A  B  C  D   E   F  G  H   I  J   K  L  M  N   ← Added as clarification

Step 1:
- Starting position A, n=3
- Output line 1: 2, 6, 9 (A-C)
- e = 9 (C)
- Distinct values already visited: 2, 6, 9
- Distinct values left to visit: -7, -4, -1, -2, 3, 5
- Amount of times we've visited the items: A=1, B=1, C=1

Step 2:
- Starting position L (9 steps forward from position C), e=9
- Output line 2: 2, 2, 3, 2, 6, 9, 2, -4, -1 (L-N + A-F)
- e = -1 (F)
- Distinct values already visited: -4, -1, 2, 3, 6, 9
- Distinct values left to visit: -7, -2, 5
- Amount of times we've visited the items: A=2, B=2, C=2, D=1, E=1, F=1, L=1, M=1, N=1

Step 3:
- Starting position E (1 step backwards from position F), e=-1
- Output line 3: -4 (E)
- e = -4 (E)
- Distinct values already visited: -4, -1, 2, 3, 6, 9
- Distinct values left to visit: -7, -2, 5
- Amount of times we've visited the items: A=2, B=2, C=2, D=1, E=2, F=1, L=1, M=1, N=1

Step 4:
- Starting position A (4 steps backwards from position E), e=-4
- Output line 4: 2, 3, 2, 2 (A + N-L)
- e = 2 (L)
- Distinct values already visited: -4, -1, 2, 3, 6, 9
- Distinct values left to visit: -7, -2, 5
- Amount of times we've visited the items: A=3, B=2, C=2, D=1, E=2, F=1, L=2, M=2, N=2

We've now visited A n (3) amount of times, so we stop.

Example 2:

n = 2
a = [2, 3, 2]
     ^  ^  ^
     A  B  C   ← Added as clarification

Step 1:
- Starting position A, n=2
- Output line 1: 2, 3 (A-B)
- e = 3 (B)
- Distinct values already visited: 2, 3
- Distinct values left to visit: none
- Amount of times we've visited the items: A=1, B=1

We've already outputted all distinct values, so we stop.

Challenge rules:

  • Input and output are flexible. Input can be an array/list of integers, string with delimiter, etc. Output can be to STDOUT, can be a 2D integer array/list, a string, etc.
  • As you may have noticed from the test cases, you wrap around the array if you're out of bounds.
  • The input n can also be larger than the size of a (i.e. n = 5, a = [2, 3, 4] will have an output of 2, 3, 4, 2, 3, and will stop there since we've outputted all distinct values already).
  • Input array can contain any reasonable integer (including those larger than the array-size) excluding 0.
  • If the input for n is 1, it means the output will always be just the first item.

General rules:

  • This is , so shortest answer in bytes wins.
    Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language.
  • Standard rules apply for your answer, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.
  • Default Loopholes are forbidden.
  • If possible, please add a link with a test for your code.
  • Also, please add an explanation if necessary.

Test cases:

Input: 3, [2, 6, 9, 2, -4, -1, 3, 5, -7, 3, -2, 2, 2, 3]
Output: [[2, 6, 9], [2, 2, 3, 2, 6, 9, 2, -4, -1], [-4], [2, 3, 2, 2]]

Input: 2, [2, 3, 2]
Output: [[2, 3]]

Input: 5, [2, 3, 4]
Output: [[2, 3, 4, 2, 3]]

Input: 1, [2, 1, -3]
Output: [[2]]

Input: 2, [1, 1, 2, -3]
Output: [[1, 1], [2], [1, 1]]

Input: 17, [1, 4, 9]
Output: [[1, 4, 9, 1, 4, 9, 1, 4, 9, 1, 4, 9, 1, 4, 9, 1, 4]]

Input: 2, [1, -2, 3, -4, 5, -6, 6, -5, 4, -3, 2, -1, 1, 1, 1]
Output: [[1, -2], [1, 1], [1]]

Input: 4, [1, -2, 3, -4, 5, -6, 6, -5, 4, -3, 2, -1, 1, 1, 1]
Output: [[1, -2, 3, -4], [1, 1, 1, -1], [2], [1, 1], [1], [1], [-2], [1, 1], [1]]

Input: 3, [-1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
Output: [[-1, 1, 2], [4, 5], [10, 11, 12, 13, -1], [13], [12, 13, -1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]

Input: 5, [5, 2, -3, 7, -3, 2, -4, 4, 4, 4]
Output: [[5, 2, -3, 7, -3], [2, 5, 4], [7, -3, 2, -4]]

Input: 2, [7, 24, 11]
Output: [[7, 24], [24, 11, 7, 24, 11, 7, 24, 11, 7, 24, 11, 7, 24, 11, 7, 24, 11, 7, 24, 11, 7, 24, 11, 7]]
\$\endgroup\$
4
\$\begingroup\$

Python 2, 149 140 bytes

n,a=input()
l=len(a)
s=set(a)
i=0
v=[n]*l
while min(v)and s:
 for x in range(i,i+n,n>0 or-1):v[x%l]-=1;n=a[x%l];s-={n};print n,
 i=x+n;print

Try it online!


  • -9 bytes, thanks to Bubbler
\$\endgroup\$
  • \$\begingroup\$ Count down from n instead of counting up from 0 to eliminate the need for e; e>0 or-1 is better than [1,-1][e<0]. TIO (140 bytes) \$\endgroup\$ – Bubbler Mar 2 '18 at 4:54
  • \$\begingroup\$ @Bubbler Thanks :) \$\endgroup\$ – TFeld Mar 2 '18 at 10:40
3
\$\begingroup\$

R, 167 163 bytes

function(n,a,l=sum(a^0),u=rep(n,l),v=0)while(!(all(a%in%v))&min(u)){for(i in F+0:(n-sign(n))){x=i%%l+1
v=c(v,a[x]);u[x]=u[x]-1}
print(tail(v,abs(n)));n=a[x];F=i+n}

Try it online!

I started with a slightly longer solution but then got inspired by TFeld's Python 2 answer which saved me 3 bytes.

−4 bytes thanks to Giuseppe.

\$\endgroup\$
  • \$\begingroup\$ use F instead of f to save another 4 bytes. \$\endgroup\$ – Giuseppe Mar 4 '18 at 16:39
  • \$\begingroup\$ @Giuseppe Cool, thanks! \$\endgroup\$ – Robert Hacken Mar 4 '18 at 18:51
2
\$\begingroup\$

Pyth, 112 109 bytes

=HQJ@H0K@H1=H.{K=bJ=Y*]0lKW&>J@.MZY0HI._bVrZ+Zb XYNh@YN=b@KN=-Hbp+bd).?Vr+ZbZ XYNh@YN=b@KN=-Hbp+bd))=Z+Nbp"\n

Try it online!

This is just a translation of TFeld's Python 2 answer. Could probably be golfed far better by someone with a better understanding of Pyth, but I figured I might as well share this anyway.

\$\endgroup\$
1
\$\begingroup\$

JavaScript (ES6), 188 bytes

(n,l)=>{for(N=l.length,b=l.map(_=>0),o=[],u=l,e=n,p=0,O=[];!o[n]*u[0];p+=j-s,e=j,O.push(g))for(s=e<0?-1:1,g=[],i=0;i<e*s;p+=s)g[i++]=j=l[p=(p%N+N)%N],o[++b[p]]=u=u.filter(k=>k-j);return O}

Try it online! Comes with a header/footer that run the test cases.

Works somewhat like TFeld's Python answer, but ended up slightly longer.

\$\endgroup\$
1
\$\begingroup\$

Perl 5, -a 123 120 116 115 114 bytes

This has the slight blemish that it prints an extra space at the start of each line

#!/usr/bin/perl -a
@;{@F}=$,=$";say$e!~/-?/,map$U{$z}=$S[++$s[$%]]=$e=$F[$%=($&.1+$%)%@F],($%+=$&.~-$')x($' or$@.=<>)until@S>$@|%U~~%

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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