Trace a sentinel-based string scan

Problem

Without running the code, determine scan_until_marker(" A1-b 2#C9"). The scan stops at the first #, ignores whitespace, adds digit values, lowercases letters, and records ? for every other character.

Reveal answer or reference solution

The result is ("a?b", 3). The leading space and the space before 2 are ignored. A and b contribute a and b; - contributes ?; 1+2=31 + 2 = 3; and no character after # is visited.

def scan_until_marker(text: str) -> tuple[str, int]:
    letters: list[str] = []
    digit_sum = 0
    for character in text:
        if character == "#":
            break
        if character.isspace():
            continue
        if character.isdigit():
            digit_sum += int(character)
        elif character.isalpha():
            letters.append(character.lower())
        else:
            letters.append("?")
    return "".join(letters), digit_sum

Public tests

  • scan_until_marker(" A1-b 2#C9") → ('a?b', 3)
  • scan_until_marker("") → ('', 0)
  • scan_until_marker("X7 Y3") → ('xy', 10)
  • scan_until_marker("9#8") → ('', 9)
Outcome

Previous attempts

Loading attempts saved in this browser…

    Review Screening Results