
Code
Shrink the Evidence
Here's the detective method that professionals use:
- Read the clue (the traceback or the wrong output).
- Reproduce it: make it happen again, on purpose.
- Shrink the input until it's tiny.
- Guess a cause, then check it with a print or an assert.
- 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
- Set
tinyto a list of at most 2 days where the currentlongest_streakgives the wrong answer. Shrinklogstep by step to find one. - Now that you see the cause, fix
longest_streak. It must work for any list, including[].
- Set
tinyto a list of at most 2 days where the currentlongest_streakgives the wrong answer. Shrinklogstep by step to find one. - Now that you see the cause, fix
longest_streak. It must work for any list, including[].
main.py Python sleeping
Press Run to try your code, then Check to test it.