Clean Invalid Token Annotations
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
annotations = [4, -2, 0, 7, -1, 3][4, 0, 7, 3]annotations = [-5, -3, -1, 0, 2, -4, 6][0, 2, 6]annotations = [][]Constraints
0 <= len(annotations) <= 100000-1000000000 <= annotations[i] <= 1000000000 for every i- Types:
annotationsis int[]; result is int[]
Function signature
def clean_token_annotations(annotations: list[int]) -> list[int]
annotationslist[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.