Arithmetic expression tokenizer
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.
Implement
tokenize(src: str) → list[list[str]]Examples
in
["x1 + 3.5 * (foo)"]out[["ID","x1"],["OP","+"],["NUM","3.5"],["OP","*"],["OP","("],["ID","foo"],["OP",")"]]What a strong answer looks like
State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.
0:00 of about 30 min
solution.py
InputExpectedGot
["x1 + 3.5 * (foo)"][["ID","x1"],["OP","+"],["NUM","3.5"],["OP","*"],["OP","("],["ID","foo"],["OP",")"]]not run yetsample