1. Lighthouse

Level 8 · Longest run of days on target

The request

From Laura Bennett, Head of Finance

“For the December review: what was our longest run of consecutive days at or above the $4,000 daily target in the first two weeks? Just the number of days.”

Your goal

Set result to the length (an int) of the longest run of consecutive days in daily_revenue whose value is at least target (4000 counts as on target).

  • Store a number in resultnot done yet

Your code

Python

Loading the editor…

    ResultPress Run to see yours

    Mote checks your result

    Press Run. Each row that matches the goal becomes a firefly in your lantern.

    The lesson

    Two accumulators at once

    A streak question needs a little more than a running total. You track two numbers as you walk through the list in order:

    • current, the length of the run you are in right now;
    • best, the longest run you have seen so far.
    best = 0
    current = 0
    for revenue in daily_revenue:
        if revenue >= target:
            current += 1          # the run continues
        else:
            current = 0           # the run is broken
        best = max(best, current)

    current += 1 is shorthand for current = current + 1. max(best, current) returns the larger of the two, so best only ever goes up. The loop has to visit the days in date order, which is why the list is already sorted from December 1 to 14. A streak on shuffled data means nothing.

    Walking through December

    Laid out day by day, the target test gives this pattern (✓ at or above $4,000):

    Dec  1  2  3  4  5  6  7  8  9 10 11 12 13 14
         .  ✓  ✓  .  ✓  ✓  ✓  ✓  .  ✓  ✓  ✓  ✓  ✓

    current climbs to 2, resets on Dec 4, climbs to 4, resets on Dec 9, and then climbs to 5 by the last day. best ends at 5. Notice that 11 of the 14 days were on target, but the question is about the longest unbroken run, so counting the ticks isn’t enough.

    Where streak code usually breaks

    The most tempting version updates best only when a run ends, inside the else. It looks tidy, but a run that is still going when the list ends never gets recorded. Here the longest run is exactly that one, Dec 10 to 14, so the tidy version answers 4. Updating best on every pass, after the if/else, handles the end of the list for free.

    The other trap is the boundary. “At or above $4,000” means >=. On Dec 12 revenue was exactly 4,000, so with > the final run splits in two and the answer drops to 4 again.

    Common mistakes

    • Updating best only when a run breaks and losing the final run.
    • Using > when the rule says “at or above”.
    • Counting all on-target days (11) instead of the longest consecutive run.
    • Forgetting to reset current to 0 on a miss, which turns the count into a plain total.

    Pick what may run on this site. You can change this any time from "Cookie settings" at the bottom of every page.