6
1
Challenge:
Input:
- An integer
n
(> 0
) - An integer-array
a
of size> 0
(which contains positive and negative values, but no zeroes)
Output:
- First output the first
n
items - Then take the last item you've outputted (let's call it
e
), and goe
steps forward ife
is positive, orabs(e)
steps backwards ife
is negative. And from that position, outputabs(e)
items (again either going forward / backwards depending on positive / negative). - 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 ofa
(i.e.n = 5, a = [2, 3, 4]
will have an output of2, 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 code-golf, 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]]
Count down from
– Bubbler – 2018-03-02T04:54:57.437n
instead of counting up from0
to eliminate the need fore
;e>0 or-1
is better than[1,-1][e<0]
. TIO (140 bytes)@Bubbler Thanks :) – TFeld – 2018-03-02T10:40:38.733