Hackbright Code Challenges

Leaping Lemur: Solution

Leaping Lemur: Solution

Problem

Leaping Lemur

Whiteboard

Medium

Challenge

Easier

Concepts

Loops, Lists

Download

leaping-lemur-solution.zip


lemur.py
def  lemur(branches):
    """Return number of jumps needed."""

    assert branches[0] == 0, "First branch must be alive"
    assert branches[-1] == 0, "Last branch must be alive"

    # START SOLUTION

    at = 0
    n_jumps = 0

    while at < len(branches) - 1:
        at += 2
        if at >= len(branches) or branches[at] == 1:
            # We can jump this far, so only jump 1
            at -= 1
        n_jumps += 1

    return n_jumps