easy +10 pts

Multiline anchor match

Extract lines that start with a given plain-text prefix using multiline anchors.

Write a function `match_lines(text: str, prefix: str) -> list[str]` that returns a list of all lines in `text` that start with the given `prefix`. A line is a sequence of characters ending with a newline (`\n`) or the end of the string. The match must be anchored to the start of each line using the `re.MULTILINE` flag. The prefix is a plain string, not a regular expression, so escape any special characters. Return the matching lines in order of appearance, without the trailing newline. If no line matches, return an empty list. For this problem, a line starts at the beginning of the string or immediately after a newline character. A line that is empty (just a newline) should not match unless the prefix is also empty. The matched line should include all characters from the start of the line up to, but not including, the newline (or the end of the string). **Function signature:** ```python def match_lines(text: str, prefix: str) -> list[str]: ```

Constraints

- 0 <= len(text) <= 10^4 - 0 <= len(prefix) <= 10^3 - The text contains only printable ASCII characters and '\n'. - Time complexity: O(n) where n is the length of text.

Example

```python
>>> match_lines('apple\nbanana\napple pie\ncherry', 'apple')
['apple', 'apple pie']
>>> match_lines('abc\ndef\nabc', 'def')
['def']
>>> match_lines('one\ntwo\nthree', 'x')
[]
```
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Use `re.findall` with a pattern that anchors at line start and matches up to the newline.
Escape the prefix with `re.escape(prefix)` to treat it literally.
Use `re.MULTILINE` so `^` matches after each newline.
Match `[^\n]*` after the prefix to capture the rest of the line without the newline.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.