← run

py-02-csv-groupby

1.000
8/8 tests· data

Proposed solution

```python
import csv
from io import StringIO

def group_sum(csv_text: str, key_col: str, val_col: str) -> dict[str, float]:
    # Remove trailing newlines and split into lines
    lines = csv_text.rstrip('\n').split('\n')
    
    # Filter out blank lines
    non_blank_lines = [line for line in lines if line.strip()]
    
    # If we only have a header or empty input, return empty dict
    if len(non_blank_lines) <= 1:
        return {}
    
    # Create a StringIO object to use with csv.reader
    csv_file = StringIO('\n'.join(non_blank_lines))
    
    # Read the CSV data
    reader = csv.DictReader(csv_file)
    
    # Initialize result dictionary
    result = {}
    
    # Process each row
    for row in reader:
        key_value = row[key_col]
        val_value = float(row[val_col])
        
        if key_value in result:
            result[key_value] += val_value
        else:
            result[key_value] = val_value
    
    return result
```

Test output (stdout)

........                                                                 [100%]
8 passed in 0.01s
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.