Longest run of ones
Return the length of the longest consecutive run of 1 values in a binary list. Empty input returns zero.
current = 2
for value in [1, 0]:
current = current+1 if value == 1 else 0
print(current)Reason about the state
Reset the current run at a zero and retain the maximum after every item. Only two counters are needed. Before writing code, identify what each variable means after one iteration. Walk through the example and explain why the next transition preserves that meaning. Then check the smallest permitted input and a case with repeated values. A correct answer follows the contract for every input, including the boundary cases in the checks. Keep any input mutation within the stated rules; return a fresh result when the task asks for one.
Your task
Return the length of the longest consecutive run of 1 values in a binary list. Empty input returns zero.
The explanation opens here once every check passes.