← run

he-017

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

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_music(music_string: str) -> List[int]:
    """ Input to this function is a string representing musical notes in a special ASCII format.
    Your task is to parse this string and return list of integers corresponding to how many beats does each
    not last.

    Here is a legend:
    'o' - whole note, lasts four beats
    'o|' - half note, lasts two beats
    '.|' - quater note, lasts one beat

    >>> parse_music('o o| .| o| o| .| .| .| .| o o')
    [4, 2, 1, 2, 2, 1, 1, 1, 1, 4, 4]
    """
```

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

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


def check(candidate):
    assert candidate('') == []
    assert candidate('o o o o') == [4, 4, 4, 4]
    assert candidate('.| .| .| .|') == [1, 1, 1, 1]
    assert candidate('o| o| .| .| o o o o') == [2, 2, 1, 1, 4, 4, 4, 4]
    assert candidate('o| .| o| .| o o| o o|') == [2, 1, 2, 1, 4, 2, 4, 2]


def test_he_017():
    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_music(music_string: str) -> List[int]:
    """ Input to this function is a string representing musical notes in a special ASCII format.
    Your task is to parse this string and return list of integers corresponding to how many beats does each
    not last.

    Here is a legend:
    'o' - whole note, lasts four beats
    'o|' - half note, lasts two beats
    '.|' - quater note, lasts one beat

    >>> parse_music('o o| .| o| o| .| .| .| .| o o')
    [4, 2, 1, 2, 2, 1, 1, 1, 1, 4, 4]
    """
    result = []
    i = 0
    while i < len(music_string):
        if music_string[i] == ' ':
            i += 1
            continue
        
        # Check for two-character notes first
        if i + 1 < len(music_string) and music_string[i:i+2] == 'o|':
            result.append(2)
            i += 2
        elif i + 1 < len(music_string) and music_string[i:i+2] == '.|':
            result.append(1)
            i += 2
        elif music_string[i] == 'o':
            result.append(4)
            i += 1
        else:
            # Skip any other characters (shouldn't happen with valid input)
            i += 1
    
    return result
```

Test output (stdout)

.                                                                        [100%]
1 passed in 0.01s