A label scanner uses the following Java code to count matching pairs of neighbouring characters. An iteration is one execution of the loop body. String indices start at zero, and charAt(i) returns the character at index i.


Java
String label = "AABBBAACCCDD";
int i = 0;
int pairs = 0;

while (i < label.length() - 1) {
    if (label.charAt(i) == label.charAt(i + 1)) {
        pairs++;
        i += 2;
    } else {
        i++;
    }
}


After a match, the traversal skips both matched characters. Determine the final value of pairs and the total number of iterations. Report both answers as exact integers.

Your answers

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

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:


Python
current = 0
longest = 0

for character in signal:
    if character == "1":
        current += 1
        if current > longest:
            longest = current
    else:
        current = 0


For 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.

SignalFinal currentFinal longest
1101110
01111011
111
Empty string

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

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:


Python
i = len(code)
zeros = 0

while code[i] == "0" and i >= 0:
    zeros += 1
    i += 1


Complete 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.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors