32
One day you awake only to find yourself caught in an array. You try to just walk out of there, taking one index at the time, but it seems there are other rules:
The array is completely filled with natural numbers.
- If you find yourself on an index
n
, you go to the indexarray[n]
, except: - If you find yourself on an index
n
which is a prime number, you takearray[n]
steps back
Example:
You start on index 4
, in this array (start index is 0):
array = [1,4,5,6,8,10,14,15,2,2,4,5,7];
-----------------^ you are here
As the value of the field you are on is 8
, you go to the index 8
as the first step. The field you land on contains the value 2
. You then go to index 2
as your second step. As2
is a prime number, you take 5 steps back, which is your third step. As there is no index -3
, you successfully escaped the array in a total of 3 steps.
Your task is:
To write a program or function, which accepts an array and a start index as parameter, and outputs the amount of steps to escape the array. If you can't escape the array (e.g. [2,0,2]
with start-index 2
=> you constantly go from index 2
to 0
), output a falsy value. You may use one-based indexing or zero-based indexing, but please specify which you use.
Test cases
Input: [2,5,6,8,1,2,3], 3
Output: 1
Input: [2, 0, 2], 2
Output: false
Input: [14,1,2,5,1,3,51,5,12,3,4,41,15,4,12,243,51,2,14,51,12,11], 5
;
Output: 6
The shortest answer wins.
7
Welcome to PPCG! This is a decent first challenge. :) Can we use 1-based indexing as well? Also it might be good to have a few more test cases. For future challenges you can also consider using the sandbox where you can get feedback from the community before a challenge goes live.
– Martin Ender – 2016-07-25T11:18:03.737Thanks for the feedback - to be honest I didn't read through the page much, so thanks for the sandbox advice, will do that next time :) I'll do an edit shortly. – Michael Kunst – 2016-07-25T11:24:50.057
Based on your second last paragraph I think I know the answer, but just in case: does "natural numbers" here mean
> 0
or>= 0
? – Sp3000 – 2016-07-25T11:26:03.523@Sp3000 It means
>= 0
– Michael Kunst – 2016-07-25T11:33:54.667Can you put a testcase which outputs 0? Or, is the start index guaranteed to be valid? – Leaky Nun – 2016-07-25T12:07:45.517
You're speaking of natural numbers
N
and you use0
in your test cases, so I assume you mean the setN= {0,1,2,...}
, right? Is it correct that we have to assumeN={1,2,3,...}
for 1-based indexing? Can you add some 1-based indexing testcases? – flawr – 2016-07-25T12:18:29.093@LeakyNun yes, you can expect that the startIndex is valid. @flawr: no, it's
N={0,1,2,3..}
for 1-based indexing aswell. You will have escaped the array if you land on index0
however. – Michael Kunst – 2016-07-25T12:20:35.843@LeakyNun no, you take
array[n]
steps back, so the value which is in index2
. In the example above this is the number5
– Michael Kunst – 2016-07-25T12:27:43.047@LeakyNun you're right, pasted the wrong array, it's fixed now – Michael Kunst – 2016-07-25T12:44:12.937
@MichaelKunst Please add some 1-indexed testcases. – flawr – 2016-07-25T12:55:23.780
1Very closely related – Peter Taylor – 2016-07-25T13:44:32.897
1@Martin Ender it's not related to the question... but me as a mobile user find it impossible to use the sandbox. What should I do to get a feedback on my questions before actually posting them? – user6245072 – 2016-07-25T18:52:26.397
What would using 1-based indexing look like? Does it affect only the initial index or all elements of the array? Would a value of 3 mean we go to the (1-based) index 3, but still go three steps back? If so, the current test cases don't cover 1-based indexing at all... – Dennis – 2016-07-25T23:37:13.777
I am confused. The third test case says start on index 5 which has the value 3 which is prime and since you can't take 3 steps back because this is the first step the answer should be 1 step, right? So how is the answer 6 and why am I the only one that is confused? – Jerry Jeremiah – 2016-07-26T01:05:49.113
1@JerryJeremiah why can't you take 3 steps back? you'll land on index 2 if you start at 5 and take 3 steps back – Michael Kunst – 2016-07-26T06:08:20.753
5@user902383 going to index 2, which is prime, so we do 2 steps back and go to index 0, which is not prime. The value at index 0 is 2, so we go to index 2, which is prime ... repeat – edc65 – 2016-07-26T09:27:07.203
Ahhh. So "X steps back" means "X indexes toward zero"? I read "As the value of the field you are on is 8, you go to the index 8 as the FIRST STEP. The field you land on contains the value 2. You then go to index 2 as your SECOND STEP." which clearly (to me) equated "step" with "move" so "X steps back" would mean "back to the index you were on X moves ago". I still think using "first step" to mean "first move" and "second step" to mean "second move" and "steps back" to mean "indexes toward zero" is confusing but at least I understand now. – Jerry Jeremiah – 2016-07-26T21:37:22.860