← run

he-001

1.000
1/1 tests· algorithms
Challenge · difficulty 3/5
# HumanEval/1

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 separate_paren_groups(paren_string: str) -> List[str]:
    """ Input to this function is a string containing multiple groups of nested parentheses. Your goal is to
    separate those group into separate strings and return the list of those.
    Separate groups are balanced (each open brace is properly closed) and not nested within each other
    Ignore any spaces in the input string.
    >>> separate_paren_groups('( ) (( )) (( )( ))')
    ['()', '(())', '(()())']
    """
```

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

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


def check(candidate):
    assert candidate('(()()) ((())) () ((())()())') == [
        '(()())', '((()))', '()', '((())()())'
    ]
    assert candidate('() (()) ((())) (((())))') == [
        '()', '(())', '((()))', '(((())))'
    ]
    assert candidate('(()(())((())))') == [
        '(()(())((())))'
    ]
    assert candidate('( ) (( )) (( )( ))') == ['()', '(())', '(()())']


def test_he_001():
    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
I'll implement the `separate_paren_groups` function that separates groups of nested parentheses.

```python
from typing import List


def separate_paren_groups(paren_string: str) -> List[str]:
    """ Input to this function is a string containing multiple groups of nested parentheses. Your goal is to
    separate those group into separate strings and return the list of those.
    Separate groups are balanced (each open brace is properly closed) and not nested within each other
    Ignore any spaces in the input string.
    >>> separate_paren_groups('( ) (( )) (( )( ))')
    ['()', '(())', '(()())']
    """
    # Remove all spaces from the input string
    cleaned_string = paren_string.replace(' ', '')
    
    result = []
    current_group = ""
    balance = 0
    
    for char in cleaned_string:
        if char == '(':
            current_group += char
            balance += 1
        elif char == ')':
            current_group += char
            balance -= 1
            
            # When we've closed a complete group
            if balance == 0:
                result.append(current_group)
                current_group = ""
    
    return result
```

This solution works by:

1. First removing all spaces from the input string
2. Iterating through each character in the cleaned string
3. Keeping track of the balance (number of open parentheses minus closed parentheses)
4. Building up each group character by character
5. When the balance reaches 0, we've completed a balanced group, so we add it to our result list and reset for the next group

The function correctly handles nested parentheses and ensures that groups are properly separated based on their balance.

Test output (stdout)

.                                                                        [100%]
1 passed in 0.01s