Challenge

Problem

E-commerce companies receive daily exports from Shopify/Square and need automated monthly reports for management. This drill teaches you to parse sales CSV data, calculate monthly revenue by category, identify top-selling products, and generate summary reports with totals, averages, and percentage breakdowns. You'll master CSV processing with type converters, hash aggregation operations, and business metric calculations.

Difficulty: Intermediate

Instructions

  1. Read sales CSV with headers (Date, Product, Category, Quantity, Unit_Price, Customer_ID)
  2. Calculate total revenue per transaction (Quantity * Unit_Price)
  3. Group transactions by month and category
  4. Calculate monthly revenue by category
  5. Identify top 3 products by total revenue
  6. Generate summary CSV with:
    • Month, Category, Revenue, Transaction_Count, Avg_Transaction
  7. Output summary statistics to console:
    • Total revenue: $X,XXX
    • Best month: Month ($X,XXX)
    • Top category: Category (X% of revenue)

Files

Editable
Read-only

Hints

Hint 1

CSV.read(file, headers: true) gives hash-like access to columns

Hint 2

Date.parse(string).strftime('%Y-%m') extracts year-month

Hint 3

Use nested hashes: Hash.new { |h, k| h[k] = Hash.new(0) }

Hint 4

Calculate revenue per transaction: quantity * unit_price

Hint 5

Use group_by to aggregate by month or category

Hint 6

transform_values applies operation to all hash values

Hint 7

max_by finds the hash entry with maximum value

Provided Files (Read-only)

1. Basic sales analysis - 2 months

Input:
analyze_sales('sales.csv')
Expected Output:
Total revenue: $3,300
Best month: 2024-01 ($2,900)
Top category: Electronics (72.7%)

2. Single transaction

Input:
analyze_sales('sales.csv')
Expected Output:
Total revenue: $250
Best month: 2024-03 ($250)
Top category: Electronics (100.0%)

3. Multiple categories same month

Input:
analyze_sales('sales.csv')
Expected Output:
Total revenue: $1,800
Best month: 2024-01 ($1,800)
Top category: Electronics (55.6%)
+ 2 hidden test cases