PracticeArrays
Strict Length Selector
Context
Consider a finite sequence of lowercase tokens and a numerical cutoff. Some tokens are longer than the cutoff, while others are not. Select exactly the qualifying tokens without changing their order or collapsing repeated values.
Problem
Implement a function that receives a sequence of tokens and an integer threshold. Traverse the sequence from left to right. Keep a token precisely when its length is strictly greater than the threshold. Return the kept tokens in encounter order, including every qualifying occurrence. Tokens whose lengths equal the threshold must not be kept. If no token qualifies, return an empty sequence.
Examples
Example 1
Input:
threshold = 3tokens = ["cat", "house", "tree", "sun"]Output:
["house", "tree"]Explanation: For the first executed example, the returned sequence is ["house", "tree"].
Example 2
Input:
threshold = 3tokens = ["blue", "green", "blue", "red"]Output:
["blue", "green", "blue"]Explanation: For the second executed example, the returned sequence is ["blue", "green", "blue"], so both qualifying occurrences of "blue" remain separate.
Example 3
Input:
threshold = 5tokens = ["sun", "moon", "star"]Output:
[]Explanation: For the third executed example, no token qualifies, so the returned sequence is [].
Constraints
0 <= len(tokens) <= 1001 <= len(tokens[i]) <= 30 for every i-10 <= threshold <= 30tokens contains only lowercase letters.- Types:
tokensis str[],thresholdis int; result is str[]
Function signature
def select_long_tokens(tokens: list[str], threshold: int) -> list[str]
tokenslist[str]- The ordered sequence of lowercase tokens to inspect.
thresholdint- The fixed length cutoff; only tokens longer than it qualify.
- returns list[str]
- The tokens whose lengths are strictly greater than threshold, in their original order and with duplicates retained.
Adapted from MBPP problem task_128 (CC BY 4.0). Rewritten, extended and verified by Iksha.
Code
Saved