← run

he-019

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

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 sort_numbers(numbers: str) -> str:
    """ Input is a space-delimited string of numberals from 'zero' to 'nine'.
    Valid choices are 'zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight' and 'nine'.
    Return the string with numbers sorted from smallest to largest
    >>> sort_numbers('three one five')
    'one three five'
    """
```

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

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


def check(candidate):
    assert candidate('') == ''
    assert candidate('three') == 'three'
    assert candidate('three five nine') == 'three five nine'
    assert candidate('five zero four seven nine eight') == 'zero four five seven eight nine'
    assert candidate('six five four three two one zero') == 'zero one two three four five six'


def test_he_019():
    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 sort_numbers(numbers: str) -> str:
    """ Input is a space-delimited string of numberals from 'zero' to 'nine'.
    Valid choices are 'zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight' and 'nine'.
    Return the string with numbers sorted from smallest to largest
    >>> sort_numbers('three one five')
    'one three five'
    """
    word_to_num = {
        'zero': 0, 'one': 1, 'two': 2, 'three': 3, 'four': 4,
        'five': 5, 'six': 6, 'seven': 7, 'eight': 8, 'nine': 9
    }
    words = numbers.split()
    sorted_words = sorted(words, key=lambda w: word_to_num[w])
    return ' '.join(sorted_words)
```

Test output (stdout)

.                                                                        [100%]
1 passed in 0.01s
Reasoning (chain-of-thought)
The user wants me to implement a function `sort_numbers` that:
1. Takes a space-delimited string of number words ('zero' through 'nine')
2. Returns them sorted from smallest to largest

Let me think about the approach:
1. Map each word to its numeric value
2. Sort based on numeric value
3. Return the sorted words as a space-delimited string

Here's my implementation:

```python
from typing import List


def sort_numbers(numbers: str) -> str:
    word_to_num = {
        'zero': 0, 'one': 1, 'two': 2, 'three': 3, 'four': 4,
        'five': 5, 'six': 6, 'seven': 7, 'eight': 8, 'nine': 9
    }
    words = numbers.split()
    sorted_words = sorted(words, key=lambda w: word_to_num[w])
    return ' '.join(sorted_words)
```

This should work correctly. Let me verify with the example:
- Input: 'three one five'
- Split: ['three', 'one', 'five']
- Sort by numeric value: ['one', 'three', 'five']
- Join: 'one three five' ✓

Edge cases:
- Empty string: split gives [], sorted gives [], join gives '' - should be fine
- Single word: works fine