easy +8 pts

Parse coordinate pairs

Parse a string of coordinate pairs into a list of lists.

Write a function `parse_coordinate_pairs(text: str) -> list` that takes a string containing zero or more coordinate pairs and returns a list of pairs, where each pair is a list `[x, y]` of integers, in the order they appear. Each coordinate pair is written as `(x,y)` where `x` and `y` are integers (possibly negative). Pairs are separated by a comma and optional whitespace. The input may have leading or trailing whitespace. Return an empty list if no coordinate pairs are found. For example, the string `"(1,2), (3,4), (5,6)"` should return `[[1,2], [3,4], [5,6]]`. You may assume the input is well-formed: every pair contains two integers, and pairs are properly comma-separated.

Constraints

The input string length is between 0 and 10,000. The integer values fit in a standard Python int. There are no nested parentheses or extra characters.

Example

>>> parse_coordinate_pairs("(1,2), (3,4), (5,6)")
[[1, 2], [3, 4], [5, 6]]
>>> parse_coordinate_pairs("(0,0),(-1,2),(10,-3)")
[[0, 0], [-1, 2], [10, -3]]
>>> parse_coordinate_pairs("")
[]
8 points ~12 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

You could split the string on commas and then process each token. But be careful: a comma also appears between the two coordinates inside a pair.
Use a regular expression to find all substrings that match `(-?\d+,-?\d+)` including optional minus signs.
For each matched pair, remove the parentheses, split on the comma, and convert to integers. Return each pair as a list.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.