Arithmetic expression tokenizer

Write codeMedium30 minStripe style

Your task

Write a tokenizer for a tiny arithmetic language. Given a source string, produce a list of [type, value] token pairs. Token types: 'NUM' for integer or decimal numbers (e.g. 12, 3.5), 'ID' for identifiers (letter or underscore followed by letters/digits/underscores), 'OP' for any single character in +-*/(), and 'WS' is skipped entirely. Any other character raises a ValueError. Numbers and identifiers are greedy (longest match). Return the list of [type, value] pairs in order.

Examples

tokenize(src: str) -> list[list[str]]
Example 1
Inputtokenize("x1 + 3.5 * (foo)")
Output[["ID", "x1"], ["OP", "+"], ["NUM", "3.5"], ["OP", "*"], ["OP", "("], ["ID", "foo"], ["OP", ")"]]