100 XP

92. Can you reach the end?

Advanced greedy lists

You stand on the first square of a row of squares. The number on each square is the longest jump you may make from it: from a square marked 3 you may move forward 1, 2 or 3 squares. A square marked 0 lets you go no further from there.

Write a function can_reach_end(jumps) that takes the list of numbers and returns True if you can reach the last square, or False if you can't.

  • With a single square you are already at the end: True.
  • Jumping as far as possible every time isn't always right: in [2, 5, 0, 0] the longest first jump lands on a 0, but a jump of 1 reaches the 5, which goes all the way.
  • Landing beyond the last square counts as reaching it.

Example 1

Inputjumps = [1, 2, 0, 1]
OutputTrue
Explanation1 step to the 2, then 2 steps to the end.

Example 2

Inputjumps = [2, 1, 0, 3]
OutputFalse
ExplanationEvery way leads to the 0 at index 2.

Example 3

Inputjumps = [0]
OutputTrue
ExplanationOne square: you start at the end.

Constraints

  • jumps has 1 to 500 whole numbers, each from 0 to 1000.

Submit also runs 9 more tests you can't see.

Code
def can_reach_end(jumps):
    """Check whether the last square can be reached.

    jumps: list of int, the longest jump from each square
    Returns: bool, True if some series of jumps from the
        first square reaches the last
    """
    # Write your code here
    pass