Code

Shrink the Evidence

Here's the detective method that professionals use:

  1. Read the clue (the traceback or the wrong output).
  2. Reproduce it: make it happen again, on purpose.
  3. Shrink the input until it's tiny.
  4. Guess a cause, then check it with a print or an assert.
  5. Fix it, and add a test so it stays fixed.

Step 3 is a superpower. A bug that shows up in a log of 1,000 days is hard to think about. So cut the input in half and check: does the bug still happen? Keep the half that fails and repeat.

The smallest input that still shows the bug is called a minimal reproducer. Often, once you see it, the cause is obvious. And it makes a perfect test.

The case

longest_streak(days) should return the longest run of True (lantern-lit) days in a row. For this log the answer is 3, but the function says 2:

log = [True, False, True, True, False, True, True, True]

Assignment

  1. Set tiny to a list of at most 2 days where the current longest_streak gives the wrong answer. Shrink log step by step to find one.
  2. Now that you see the cause, fix longest_streak. It must work for any list, including [].