PracticeArrays
Count Tokens Before the Archive Marker
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:
2Explanation: 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:
3Explanation: 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:
0Explanation: The first token is archive, so no token is counted and the executed result is 0. (fact_ids:,, )
Constraints
0 <= len(tokens) <= 2001 <= len(tokens[i]) <= 20 for every itokens[i]: alphabet lowercaseEach token contains only lowercase English letters.The exact token "archive" is the terminal marker.- Types:
tokensis str[]; result is int
Function signature
def count_active_tokens(tokens: list[str]) -> int
tokenslist[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.
Code
Saved