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 task is to implement a function that filters a list of strings to only include those that contain a given substring.
Looking at the examples:
- `filter_by_substring([], 'a')` returns `[]` (empty list)
- `filter_by_substring(['abc', 'bacd', 'cde', 'array'], 'a')` returns `['abc', 'bacd', 'array']`
So I need to:
1. Iterate through the list of strings
2. Check if each string contains the substring
3. Return a new list with only the strings that contain the substring
The simplest way to check if a string contains a substring in Python is using the `in` operator.
Let me write the 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]
```
This is a clean, Pythonic solution using list comprehension. It checks if the substring is in each string and includes it in the result if true.