Back to Practice
#0164

CSV Totals by Category

MediumPython25 min20 XP

Problem

Given CSV text with columns category and amount, return a dictionary mapping each category to its total amount.

Why This Matters

Small CSV aggregation scripts are common in operations, analytics, and interview take-home tasks.

Function Signature

def totals_by_category(csv_text):

Examples

Example 1
Input"category,amount\nbook,10\nfood,5\nbook,7"
Output{'book': 17.0, 'food': 5.0}

The two book rows are added together.

Constraints

  • Return the exact requested output.
  • Handle ordinary edge cases.
  • Prefer simple, interview-readable code.
CodePython
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Core example
Input"category,amount\nbook,10\nfood,5\nbook,7"
Expected{'book': 17.0, 'food': 5.0}

The two book rows are added together.

Hidden Test Categories
empty or minimal inputduplicates or tieslarger input