he-007
1.000
Challenge · difficulty 1/5
# HumanEval/7
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_substring(strings: List[str], substring: str) -> List[str]:
""" Filter an input list of strings only for ones that contain given substring
>>> filter_by_substring([], 'a')
[]
>>> filter_by_substring(['abc', 'bacd', 'cde', 'array'], 'a')
['abc', 'bacd', 'array']
"""
```
<!-- imported from OpenAI HumanEval (HumanEval/7) -->
tests/test_he_007.py
# Auto-generated from OpenAI HumanEval HumanEval/7. Do not edit by hand.
from solution import * # noqa: F401,F403 (prompt helpers may be referenced by tests)
from solution import filter_by_substring 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']
assert candidate(['xxx', 'asd', 'aaaxxy', 'john doe', 'xxxAAA', 'xxx'], 'xx') == ['xxx', 'aaaxxy', 'xxxAAA', 'xxx']
assert candidate(['grunt', 'trumpet', 'prune', 'gruesome'], 'run') == ['grunt', 'prune']
def test_he_007():
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_substring(strings: List[str], substring: str) -> List[str]:
""" Filter an input list of strings only for ones that contain given substring
>>> filter_by_substring([], 'a')
[]
>>> filter_by_substring(['abc', 'bacd', 'cde', 'array'], 'a')
['abc', 'bacd', 'array']
"""
return [s for s in strings if substring in s]
```Test output (stdout)
. [100%]
1 passed in 0.01s
Reasoning (chain-of-thought)
The user wants a Python function `filter_by_substring` that takes a list of strings and a substring, and returns a new list containing only the strings from the input list that contain the given substring.
The function signature is:
```python
def filter_by_substring(strings: List[str], substring: str) -> List[str]:
```
I can implement this using a list comprehension:
```python
def filter_by_substring(strings: List[str], substring: str) -> List[str]:
return [s for s in strings if substring in s]
```
Let's check the examples:
`filter_by_substring([], 'a')` -> `[]`
`filter_by_substring(['abc', 'bacd', 'cde', 'array'], 'a')` -> `['abc', 'bacd', 'array']`
Matches exactly.
I will write the complete code in `solution.py`.
No extra fluff.
Done.
I'll output the code block.
Check imports: `from typing import List` is given. I'll include it.
The prompt says "Keep the given name and signature; your file must define it at module level."
Code:
```python
from typing import List
def filter_by_substring(strings: List[str], substring: str) -> List[str]:
return [s for s in strings if substring in s]
```
That's it.
I will produce the output.
Wait, should I include the docstring? The prompt says "completes the function below. Keep the given name and signature". I'll keep the docstring as well to be safe, or just the implementation. It doesn't matter much. I'll include the docstring.
Final check of the prompt: "Implement a file **`solution.py`** that completes the function below."
I will provide exactly that.
Done.
Output matches requirements.✅
Proceeds.
Output generation.
```python
from typing import List
def filter_by_substring(strings: List[str], substring: str) -> List[str]:
""" Filter an input list of strings only for ones that contain given substring
>>> filter_by_substring([], 'a')
[]
>>> filter_by_substring(['abc', 'bacd', 'cde', 'array'], 'a')
['abc', 'bacd', 'array']
"""
return [s for s in strings if substring in s]
```
Looks good.✅
Done.
Self-Correction/Verification during thought:
Is there any edge case? Empty substring? The problem doesn't specify, but `'' in s` is True for all strings, which is standard Python behavior. The examples are straightforward. List comprehension is optimal and Pythonic.
All good.
Output matches.
Proceeds.
[Output Generation] -> *Proceeds*