← run

he-004

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

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 mean_absolute_deviation(numbers: List[float]) -> float:
    """ For a given list of input numbers, calculate Mean Absolute Deviation
    around the mean of this dataset.
    Mean Absolute Deviation is the average absolute difference between each
    element and a centerpoint (mean in this case):
    MAD = average | x - x_mean |
    >>> mean_absolute_deviation([1.0, 2.0, 3.0, 4.0])
    1.0
    """
```

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

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


def check(candidate):
    assert abs(candidate([1.0, 2.0, 3.0]) - 2.0/3.0) < 1e-6
    assert abs(candidate([1.0, 2.0, 3.0, 4.0]) - 1.0) < 1e-6
    assert abs(candidate([1.0, 2.0, 3.0, 4.0, 5.0]) - 6.0/5.0) < 1e-6


def test_he_004():
    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 mean_absolute_deviation(numbers: List[float]) -> float:
    """ For a given list of input numbers, calculate Mean Absolute Deviation
    around the mean of this dataset.
    Mean Absolute Deviation is the average absolute difference between each
    element and a centerpoint (mean in this case):
    MAD = average | x - x_mean |
    >>> mean_absolute_deviation([1.0, 2.0, 3.0, 4.0])
    1.0
    """
    if not numbers:
        return 0.0
    
    # Calculate the mean
    mean = sum(numbers) / len(numbers)
    
    # Calculate the sum of absolute deviations from the mean
    total_deviation = sum(abs(x - mean) for x in numbers)
    
    # Return the mean absolute deviation
    return total_deviation / len(numbers)
```

Test output (stdout)

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

1. I need to calculate the Mean Absolute Deviation (MAD) of a list of numbers
2. MAD = average of |x - mean| for all x in the dataset
3. The function signature is already given: `mean_absolute_deviation(numbers: List[float]) -> float`

Let me trace through the example:
- Input: [1.0, 2.0, 3.0, 4.0]
- Mean = (1.0 + 2.0 + 3.0 + 4.0) / 4 = 10.0 / 4 = 2.5
- Absolute deviations:
  - |1.0 - 2.5| = 1.5
  - |2.0 - 2.5| = 0.5
  - |3.0 - 2.5| = 0.5
  - |4.0 - 2.5| = 1.5
- Sum of absolute deviations = 1.5 + 0.5 + 0.5 + 1.5 = 4.0
- MAD = 4.0 / 4 = 1.0

This matches the expected output!

Now let me implement the solution:

```python
from typing import List


def mean_absolute_deviation(numbers: List[float]) -> float:
    """ For a given list of input numbers, calculate Mean Absolute Deviation
    around the mean of this dataset.
    Mean Absolute Deviation is the average absolute difference between each
    element and a centerpoint (mean in this case):
    MAD = average | x - x_mean |
    >>> mean_absolute_deviation([1.0, 2.0, 3.0, 4.0])
    1.0
    """
    if not numbers:
        return 0.0
    
    # Calculate the mean
    mean = sum(numbers) / len(numbers)
    
    # Calculate the sum of absolute deviations from the mean
    total_deviation = sum(abs(x - mean) for x in numbers)
    
    # Return the mean absolute deviation
    return total_deviation / len(numbers)
```

I'll verify the implementation handles edge cases and matches the expected calculation method.