PracticeArrays

Clean Invalid Token Annotations

EasyArraystraversalstate trackingfiltering Time not estimated Not started

Context

A document index stores one signed annotation score for each token. Negative scores are invalid markers left by an earlier parsing pass, while zero and positive scores are usable. Clean the list directly so the index can reuse the same storage without changing the order of valid token annotations.

Problem

Implement a function that receives a mutable list of integer token annotations. Remove every negative value from that list, keep every zero and positive value, and preserve the relative order of all retained values. The input list itself must be updated, and the function must return that same list after cleanup. Do not use arithmetic to transform values; each item is only classified by whether it is below zero. The function must also handle an empty list and adjacent negative values correctly.

Examples

Example 1
Input: annotations = [4, -2, 0, 7, -1, 3]
Output: [4, 0, 7, 3]
Explanation: For [4, -2, 0, 7, -1, 3], the executed result is [4, 0, 7, 3]. This removes the negative values and keeps the retained values in their shown order.
Example 2
Input: annotations = [-5, -3, -1, 0, 2, -4, 6]
Output: [0, 2, 6]
Explanation: For [-5, -3, -1, 0, 2, -4, 6], the executed result is [0, 2, 6]. This removes the negative values while retaining the zero and positive values.
Example 3
Input: annotations = []
Output: []
Explanation: For an empty list, the executed result is an empty list. The empty-list case is covered by the requirements.

Constraints

  • 0 <= len(annotations) <= 100000
  • -1000000000 <= annotations[i] <= 1000000000 for every i
  • Types: annotations is int[]; result is int[]

Function signature

def clean_token_annotations(annotations: list[int]) -> list[int]
annotations list[int]
The mutable list of signed annotation values, in document-token order.
returns list[int]
The updated annotations list, containing only nonnegative values in their original relative order.

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

Notes

  • The returned list must be the supplied list after mutation.
  • A value of zero is valid and must be retained.
Approved · c006r1-abs_252_v214 execution-verified tests
Code
Saved
Checking environment…Python SandboxLn 1, Col 1