The challenge
You have an array of integers and have a frog at the first position
[Frog, int, int, int, ..., int]
The integer itself may tell you the length and the direction of the jump
For instance:
1
2
3

2 = jump two indices to the right
3 = jump three indices to the left
0 = stay at the same position

Your objective is to find how many jumps are needed to jump out of the array.
Return 1
if Frog can’t jump out of the array
Example:
1
2

array = [1, 2, 1, 5];
jumps = 3 (1 > 2 > 5 > <jump out>)

The solution in Python code
Option 1:
1
2
3
4
5
6
7
8
9

def solution(a):
if not a: return 1
posSet, i = set(), 0
while i not in posSet:
posSet.add(i)
i += a[i]
if not (0 <= i < len(a)):
return len(posSet)
return 1

Option 2:
1
2
3
4
5
6
7
8
9
10
11
12

def solution(a):
steps = 0
index = 0
length = len(a)
indexes = set()
while 0 <= index < length:
if index in indexes:
return 1
indexes.add(index)
index += a[index]
steps += 1
return steps

Option 3:
1
2
3
4

def solution(a):
c = i = 0
while i >= 0 and i < len(a) and c<20 : i += a[i] ; c += 1
return [c,1][c==20]

Test cases to validate our solution
1
2
3
4
5
6
7
8
9
10

import test
from solution import solution
@test.describe("Sample tests")
def _():
@test.it("Tests")
def __():
test.assert_equals(solution([1, 2, 2, 1]), 4)
test.assert_equals(solution([1, 2, 1, 5]), 3)
test.assert_equals(solution([1, 1]), 1)
