← run

he-006

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

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 parse_nested_parens(paren_string: str) -> List[int]:
    """ Input to this function is a string represented multiple groups for nested parentheses separated by spaces.
    For each of the group, output the deepest level of nesting of parentheses.
    E.g. (()()) has maximum two levels of nesting while ((())) has three.

    >>> parse_nested_parens('(()()) ((())) () ((())()())')
    [2, 3, 1, 3]
    """
```

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

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


def check(candidate):
    assert candidate('(()()) ((())) () ((())()())') == [2, 3, 1, 3]
    assert candidate('() (()) ((())) (((())))') == [1, 2, 3, 4]
    assert candidate('(()(())((())))') == [4]


def test_he_006():
    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 parse_nested_parens(paren_string: str) -> List[int]:
    """ Input to this function is a string represented multiple groups for nested parentheses separated by spaces.
    For each of the group, output the deepest level of nesting of parentheses.
    E.g. (()()) has maximum two levels of nesting while ((())) has three.

    >>> parse_nested_parens('(()()) ((())) () ((())()())')
    [2, 3, 1, 3]
    """
    groups = paren_string.split()
    result = []
    
    for group in groups:
        current_level = 0
        max_level = 0
        for char in group:
            if char == '(':
                current_level += 1
                max_level = max(max_level, current_level)
            elif char == ')':
                current_level -= 1
        result.append(max_level)
    
    return result
```

Test output (stdout)

.                                                                        [100%]
1 passed in 0.01s
Reasoning (chain-of-thought)
Let me understand the problem:

1. We have a string with multiple groups of nested parentheses separated by spaces
2. For each group, we need to find the deepest level of nesting
3. Return a list of these maximum nesting levels

For example:
- `(()())` - Let's trace through:
  - `(` - level 1
  - `(` - level 2
  - `)` - level 1
  - `(` - level 2
  - `)` - level 1
  - `)` - level 0
  - Maximum level reached: 2

- `((()))` - Let's trace through:
  - `(` - level 1
  - `(` - level 2
  - `(` - level 3
  - `)` - level 2
  - `)` - level 1
  - `)` - level 0
  - Maximum level reached: 3

- `()` - level 1
- `((())()())` - Let's trace through:
  - `(` - level 1
  - `(` - level 2
  - `(` - level 3
  - `)` - level 2
  - `)` - level 1
  - `(` - level 2
  - `)` - level 1
  - `(` - level 2
  - `)` - level 1
  - `)` - level 0
  - Maximum level reached: 3

So the algorithm is:
1. Split the string by spaces to get individual groups
2. For each group, track the current nesting level and maximum level reached
3. Return the list of maximum levels

Let me write the solution:

```python
from typing import List


def parse_nested_parens(paren_string: str) -> List[int]:
    """ Input to this function is a string represented multiple groups for nested parentheses separated by spaces.
    For each of the group, output the deepest level of nesting of parentheses.
    E.g. (()()) has maximum two levels of nesting while ((())) has three.

    >>> parse_nested_parens('(()()) ((())) () ((())()())')
    [2, 3, 1, 3]
    """
    groups = paren_string.split()
    result = []
    
    for group in groups:
        current_level = 0
        max_level = 0
        for char in group:
            if char == '(':
                current_level += 1
                max_level = max(max_level, current_level)
            elif char == ')':
                current_level -= 1
        result.append(max_level)
    
    return result
```

I'll verify the logic by tracing through the example input. The function splits the string into groups, then tracks the maximum nesting level for each group by incrementing on '(' and decrementing on ')'.

The key is maintaining a running level counter and capturing the peak depth reached during each group's traversal. This approach efficiently calculates the maximum nesting for each parenthetical group.