← run

he-029

1.000
1/1 tests· basic
Challenge · difficulty 1/5
# HumanEval/29

Implement a file **`solution.py`** that completes the function below. Keep the given name and signature; your file must define it at module level.

```python
from typing import List


def filter_by_prefix(strings: List[str], prefix: str) -> List[str]:
    """ Filter an input list of strings only for ones that start with a given prefix.
    >>> filter_by_prefix([], 'a')
    []
    >>> filter_by_prefix(['abc', 'bcd', 'cde', 'array'], 'a')
    ['abc', 'array']
    """
```

<!-- imported from OpenAI HumanEval (HumanEval/29) -->
tests/test_he_029.py
# Auto-generated from OpenAI HumanEval HumanEval/29. Do not edit by hand.
from solution import *  # noqa: F401,F403 (prompt helpers may be referenced by tests)
from solution import filter_by_prefix as candidate

METADATA = {
    'author': 'jt',
    'dataset': 'test'
}


def check(candidate):
    assert candidate([], 'john') == []
    assert candidate(['xxx', 'asd', 'xxy', 'john doe', 'xxxAAA', 'xxx'], 'xxx') == ['xxx', 'xxxAAA', 'xxx']


def test_he_029():
    check(candidate)
System prompt
You are an expert programmer. Solve the task exactly as specified. Output your solution as fenced code blocks using the required file name(s) and the exact function/type signatures requested. Prefer correctness; do not include prose outside code unless asked.
Proposed solution
```python
from typing import List

def filter_by_prefix(strings: List[str], prefix: str) -> List[str]:
    return [s for s in strings if s.startswith(prefix)]
```

Test output (stdout)

.                                                                        [100%]
1 passed in 0.01s
Reasoning (chain-of-thought)
The user wants a Python function `filter_by_prefix` that takes a list of strings and a prefix string, and returns a new list containing only the strings from the input list that start with the given prefix.

I need to implement this in `solution.py`.
The function signature is:
```python
from typing import List

def filter_by_prefix(strings: List[str], prefix: str) -> List[str]:
```

Implementation details:
Iterate through `strings`, check if each string starts with `prefix` using `str.startswith(prefix)`, and collect those that do.
Return the collected list.

Example:
`filter_by_prefix(['abc', 'bcd', 'cde', 'array'], 'a')` -> `['abc', 'array']`

Code:
```python
from typing import List

def filter_by_prefix(strings: List[str], prefix: str) -> List[str]:
    return [s for s in strings if s.startswith(prefix)]
```

Check edge cases:
- Empty list: returns `[]`
- Empty prefix: `startswith('')` is True for all strings, so returns all strings. This matches standard behavior.
- Strings shorter than prefix: `startswith` handles this correctly (returns False).

The problem statement matches HumanEval/29.
I will output the complete code in a fenced block.
No extra text.
Done. 
Let's double check the exact prompt: "Implement a file **`solution.py`** that completes the function below. Keep the given name and signature; your file must define it at module level."

I will provide exactly that.
Code structure:
```python
from typing import List

def filter_by_prefix(strings: List[str], prefix: str) -> List[str]:
    return [s for s in strings if s.startswith(prefix)]
```
That's it.
Proceeds. 
Output matches requirement.✅
I'll generate it now. 
Self-Correction/Verification during thought:
Is there any trick? HumanEval problems are usually straightforward. `startswith` is the standard way. List comprehension is Pythonic. Type hints are provided. All good.
Output matches. 
Proceeds. 
[Output Generation] -> Fenced code block with `solution.py` content.
Done. 
```python
from typing import List

def filter_by_prefix(strings: List[str], prefix: str) -> List[str]:
    return [s for s in strings if s.startswith(prefix)]
```
Matches exactly. No extra fluff.
Done. 
Output Generation. 
(Self-Correction/Note during generation prep: The prompt asks to "Implement a file solution.py that completes the function below." I will just provide the code block.)
All good. 
Proceeds. 
[Output] -> Code block. 
Done. 
[Final Check]
Function name: `filter_by_prefix`
Arguments: `strings: List[str], prefix: str`
Return: `List[str]`
Logic: filters by prefix.
Matches docstring examples.
Ready. 
Output matches response. 
Proceeds. 
[Output Generation] (done)