#!/usr/bin/env python3
"""
Calculate statistics from numbers generated by LLM.
Expects context['numbers_str'] containing comma-separated numbers.
"""

import statistics

# Get the numbers from context (provided via parameter substitution)
numbers_str = context.get('numbers_str', '')

# Parse the numbers
try:
    numbers = [int(x.strip()) for x in numbers_str.split(',') if x.strip()]
    
    if not numbers:
        raise ValueError("No valid numbers found")
    
    # Calculate statistics
    mean = statistics.mean(numbers)
    median = statistics.median(numbers)
    stdev = statistics.stdev(numbers) if len(numbers) > 1 else 0
    
    result = {
        "numbers": numbers,
        "count": len(numbers),
        "mean": round(mean, 2),
        "median": median,
        "standard_deviation": round(stdev, 2),
        "min": min(numbers),
        "max": max(numbers)
    }
    
    print(f"Statistics for {numbers}:")
    print(f"Mean: {result['mean']}")
    print(f"Median: {result['median']}")
    print(f"Std Dev: {result['standard_deviation']}")
    
except ValueError as e:
    print(f"Error parsing numbers: {e}")
    result = {"error": str(e)}