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, hash aggregation operations, and business metric calculations. Note: This drill parses CSV manually using string methods rather than Ruby's CSV library.

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. 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

Parse CSV manually: line.split(',')

Hint 2

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

Hint 3

Calculate revenue per transaction: quantity.to_i * unit_price.to_f

Hint 4

Use a hash to accumulate totals: hash[key] ||= 0; hash[key] += value

Hint 5

max_by { |k, v| v } finds the entry with maximum value

Hint 6

Format currency: num.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\1,').reverse

Ruby 3.4

Provided Files (Read-only)

1. Correct total revenue

Input:
analyze_sales('sales.csv')
Expected Output:
Total revenue: $4,375
Best month: 2024-01 ($2,875)
Top category: Electronics (92.0%)

2. Best month identified

Input:
analyze_sales('sales.csv')
puts 'Month verified'
Expected Output:
Total revenue: $4,375
Best month: 2024-01 ($2,875)
Top category: Electronics (92.0%)
Month verified

3. Top category percentage

Input:
analyze_sales('sales.csv')
puts 'Category verified'
Expected Output:
Total revenue: $4,375
Best month: 2024-01 ($2,875)
Top category: Electronics (92.0%)
Category verified
+ 2 hidden test cases