A sensor stores its readings as a Python string containing only "0" and "1". The following code finds the longest uninterrupted run of "1" characters:
current = 0
longest = 0
for character in signal:
if character == "1":
current += 1
if current > longest:
longest = current
else:
current = 0For each signal in the table, run the entire code independently and enter the final values of current and longest as exact integers. The empty string contains no characters.
| Signal | Final current | Final longest |
| 1101110 | ||
| 01111011 | ||
| 111 | ||
| Empty string |
A device reads an identification string named code containing only decimal digits. A programmer wants to count consecutive "0" characters at its right-hand end, stopping at the first different character. The code must also work when the string is empty.
This Python draft contains three independent traversal errors:
i = len(code)
zeros = 0
while code[i] == "0" and i >= 0:
zeros += 1
i += 1Complete the repair instructions. Use a single integer in each numerical blank, and use either before or after in the remaining blank.
- Initialize i by subtracting from len(code).
- In the and condition, place i >= 0 code[i] == "0".
- After counting a zero, subtract from i to visit the next character toward the left.