PracticeArrays

Count Tokens Before the Archive Marker

Easy/MediumArraystraversalaccumulationstate tracking Time not estimated Not started

Context

A document indexer reads a list of lowercase tokens from the beginning of a note. The token "archive" marks the point where active content ends. Count the consecutive tokens before that marker so the indexer knows how much text remains active.

Problem

Implement a function that returns the number of consecutive tokens at the start of tokens that are not exactly "archive". Inspect tokens from left to right. Stop as soon as the first "archive" token is found; do not count that marker or inspect later tokens for the result. If the marker never appears, count every token. An empty token list has a count of zero.

Examples

Example 1
Input: tokens = ["draft", "notes", "archive", "summary"]
Output: 2
Explanation: The first two tokens are leading non-marker tokens, so the executed result is 2 when the third token is archive. (fact_ids:, )
Example 2
Input: tokens = ["alpha", "alpha", "review"]
Output: 3
Explanation: All three tokens are counted because the executed input contains no archive token, so the result is 3. (fact_ids:,, )
Example 3
Input: tokens = ["archive", "intro", "archive"]
Output: 0
Explanation: The first token is archive, so no token is counted and the executed result is 0. (fact_ids:,, )

Constraints

  • 0 <= len(tokens) <= 200
  • 1 <= len(tokens[i]) <= 20 for every i
  • tokens[i]: alphabet lowercase
  • Each token contains only lowercase English letters.
  • The exact token "archive" is the terminal marker.
  • Types: tokens is str[]; result is int

Function signature

def count_active_tokens(tokens: list[str]) -> int
tokens list[str]
A list of lowercase document tokens, possibly empty.
returns int
The length of the maximal initial sequence of tokens that are not exactly "archive".

Adapted from MBPP problem task_941 (CC BY 4.0). Rewritten, extended and verified by Iksha.

Approved · t3-abs_341_v214 execution-verified tests
Code
Saved
Checking environment…Python SandboxLn 1, Col 1